u/RomfordNavy

Local variables changed during exec() in Python 3.14 are not persistant

There seems to be a problem with changing a local variable within exec():

test = "test1"
exec("test = 'test2'", {}, {"test": test})
print(test) # <- erroneously returns test1

returns 'test1' - the change to 'test' during exec() has not persisted

However if passed inside a dict variable:

localDict = {"test": "test1"}
exec("test = 'test2'", {}, localDict)
test = localDict["test"]
print(test) # <- correctly returns test2

It seems to work for some reason and returns 'test2'!

Is this a bug in exec() in Python 3.14?

reddit.com
u/RomfordNavy — 1 day ago

Accessing source code from importlib module

I notice that the source code from an importlib module can be read using inspect module. However is there an alternative way of accessing this code so that it can be modified? Presumably inspect.getsource must have some method of finding this.

import inspect
import importlib
spec = importlib.util.spec_from_file_location(module_name, source_path)
module = importlib.util.module_from_spec(spec)
print(inspect.getsource(module))
reddit.com
u/RomfordNavy — 5 days ago

Compile only if .pyc is out-of-date?

Is there some way I can compile a .py script into a .pyc but only if it is missing or out-of-date with the.py file?

I am trying to compile the file if necessary and then run it from within Python, can exec() run a .pyc file?

import py_compile
py_compile.compile('Child.py')
exec(open('Child.py').read())

This works but recompiles the file on every run.

reddit.com
u/RomfordNavy — 10 days ago

How to call a *.py from within Python along with a variable?

What is the correct way to call a *.py program script from within Python while making a variable from the parent available.

Parent.py

class cParent:
    test = "test"
oParent = cParent()
import Child

Child.py

print(oParent.test)     # this line fails presumably because oParent is out of scope
reddit.com
u/RomfordNavy — 12 days ago

Create .pyc files for simple scripts

It it possible to configure Python to create a .pyc bytecode file for all simple *.py files which are run? It seems that by default it only creates a .pyc for any classes which is imported not for a program file itself.

reddit.com
u/RomfordNavy — 13 days ago

Well, had a good listening session today and here are my thoughts:

  • Fiio FT7 - quite clinical, vocals sounded 'dry'
  • Meze 105 - much warmer midrange but maybe lacked the spacial seperation and overall range of the Fiio, also very tight clamping force
  • Audeze MM-100 - similar to the Meze, lacking the detail and overall range of the Fiio
  • Hifiman Arya Organic - beautiful vocals but big hole in the treble (2khz?)
  • Hifiman HE1000 Stealth - Hifiman have fixed the hole in the treble here but the result is just way too bright, they make even the FT7 sound warm and not so analytical
  • Dan Clark Noire XO - seemed to lack punch and dynamism, not as impressed as I thought I would be with them
reddit.com
u/RomfordNavy — 14 days ago

Somewhere recently I saw an example of what was probably a WSGI server which had code to run the *.py file which had been requested, however can't seem to find that again. Lots of examples of simple WSGI servers but not the example I was looking for, anyone know where I might find that example?

reddit.com
u/RomfordNavy — 15 days ago

Coming from the Foxpro world we used Textmerge to output multiline text such as HTML, is there a similar method available in Python?

In Fox an example might be:

Use Customer
Set Textmerge On
\<html>
\<head>
\<title>HTML from FoxPro</title>
\</head>
\<body>
\<h1>Customer List</h1>
\<hr />
Scan
   \Company name is <<Upper(Company)>>
   \<br />
Endscan
\</body>
\</html>
Set Textmerge Off
Use In Customer

reddit.com
u/RomfordNavy — 24 days ago

Can anyone tell me how to use Python to return a simple HTML page using something akin to the ASP Request/Response objects please?

There are lots of examples using various frameworks but can't find anything showing how to just do something very basic.

reddit.com
u/RomfordNavy — 24 days ago