News for hackers. Stuff that matters.
Re:No-op statements syntactically valid only since Python X.Y by DemocracyFTW2 on Monday June 06, 2022 @05:55AM (#31638659)
Some things that should be easy are ridiculously hard in Python. Regexes for one have a horrible API story. Another one is the import system which I view as a totally broken effort to heal self-inflicted complexity with more complexity. Here is the code I came up when presented with the task to import a module when its name is given:

    def module_from_path( ctx, name, path ):
      ### thx to https://stackoverflow.com/a/50395128/7568091 ###
      ### thx to https://stackoverflow.com/a/67692/7568091 ###
      import importlib
      import importlib.util
      spec                      = importlib.util.spec_from_file_location( name, path )
      module                    = importlib.util.module_from_spec( spec )
      sys.modules[ spec.name ]  = module
      spec.loader.exec_module( module )
      return importlib.import_module( name )
Details may change any time without notice. The complexity here is a consequence of Python's design decision to treat module names as identifiers, not as quoted strings. The import system is full of such poor choices (dot/relative imports, `__init__.py`, `*.pyc` / `*.pyo` files littering one's source directories, and so on)
Re:No-op statements syntactically valid only since Python X.Y by tails4e on Monday June 06, 2022 @07:20AM (#31639076)
Agree on regex. I started in perl many years ago and loved how integrated regexes were. Was appalled that was was a simple one liner in perl turned into multiple lines in python for every regex. Making a powerful tool easy to use means it will be used more.

It would be nice if that flaw could be fixed, as I like coding in python in general

Re:No-op statements syntactically valid only since Python X.Y by eesmith on Monday June 06, 2022 @11:49AM (#31642005)
Here' an ancient hack that showed how that might look like - http://dalkescientific.com/Python/python4ply-tutorial.html#m... .

  count = 0
  t1 = time.time()
  for line in open("nucleosome.pdb"):
    if line =~ m/(ATOM  |HETATM)/:
        count += 1
  print count, "atoms in", time.time()-t1, "seconds"
It used it own PLY-based parser to generate the Python AST.

None of it would work now. :)

Re:No-op statements syntactically valid only since Python X.Y by eesmith on Monday June 06, 2022 @02:54PM (#31644263)
I realize now how much of a hack it is. The portion 'm/(ATOM |HETATM)/' could be part of valid Python expression, as:

  >>> ATOM = HETATM = 1
  >>> m = 2
  >>> m/(ATOM  |HETATM)/1
  2.0
so either m// is enabled only after =~, or there has to be some way to recognize the lexically correct match term -- something more powerful than the "m/[^/]*/[a-z]*" used here.
Re:No-op statements syntactically valid only since Python X.Y by dvdkon on Monday June 06, 2022 @09:33AM (#31640220)
I have to say I really like Python's module system. Modules as objects, scoped import by default and filesystem-backed module resolution (+sys.path) are all good choices in my opinion. Cosmetic warts like __pycache__ are unfortunate, but not deal-breakers.