Python sys.meta_path and strange module requests - python-2.7

Importing is behaving inconsistently and I'm pretty sure it's because of my meta_path loader.
My importer
class JaPyCodeImporter(object):
def __init__(self, modules):
self._modules = {}
self._last_module = None
modules = dict(modules)
for key in modules:
module_info = {}
module_info["full_name"] = key
module_info["source"] = modules[key]
module_info["code"] = compile(modules[key], key, "exec")
module_info["is_package"] = False
module_info["package"] = key.rpartition('.')[0]
if not key.rfind("__init__") == -1:
module_info["full_name"] = key[:key.rfind(".__init__")]
module_info["is_package"] = True
module_info["package"] = module_info["full_name"]
self._modules[module_info["full_name"]] = module_info
self._last_path = ""
def find_module(self, fullname, path):
if fullname in self._modules.keys():
print("Found: {0}".format(fullname))
return self
elif self._last_module and self._last_module["package"] + "." + fullname in self._modules.keys():
print("Found: {0} in package {1}".format(fullname, self._last_module["package"]))
return self
print("Couldn't find: {0}".format(fullname))
pdb.traceback.print_stack()
return None
def load_module(self, fullname):
expanded_name = self._last_path + "." + fullname
# check for pre-existence
if fullname in sys.modules:
return sys.modules[fullname]
elif self._last_module and self._last_module["package"] + "." + fullname in sys.modules:
return sys.modules[self._last_module["package"] + "." + fullname]
# check internally
if not fullname in self._modules.keys():
if self._last_module and not self._last_module["package"] + "." + fullname in self._modules.keys():
raise ImportError(fullname)
else:
fullname = self._last_module["package"] + "." + fullname
module_info = self._modules[fullname]
new_module = sys.modules.setdefault(fullname, imp.new_module(fullname))
new_module.__loader__ = self
new_module.__file__ = "<%s>" % self.__class__.__name__
new_module.__path__ = [] #module_info["package"].split(".")
new_module.__package__ = module_info["package"]
print("Loaded: {0}".format(module_info["full_name"]))
try:
self._last_module = self._modules[fullname]
print("Execute: {0}".format(module_info["full_name"]))
globals()["__name__"] = module_info["full_name"]
globals()["__package__"] = module_info["package"]
exec(self._modules[fullname]["code"], globals(), new_module.__dict__)
except SyntaxError as err:
error_class = err.__class__.__name__
detail = err.args[0]
line_number = err.lineno
print("%s at line %d of %s: %s" % (error_class, line_number, description, detail))
del sys.modules[fullname]
return None
except Exception as err:
error_class = err.__class__.__name__
detail = err.args[0]
print(detail)
cl, exc, tb = sys.exc_info()
line_number = traceback.extract_tb(tb)[-1][1]
print("%s at line %d of %s: %s" % (error_class, line_number, fullname, detail))
del sys.modules[fullname]
return None
return new_module
My file structure
gameSystem/
__init.__py
interface_bridge.py
libraries/
__init__.py
standard/
__init__.py
area.py
base_object.py
exit.py
player.py
Start of interface_bridge.py
print("*** begin interface_bridge.py ***")
# from https://github.com/jython/jython/blob/0a58cc26566d2b2334e80b2b3f2f42f6c738db2d/lib-python/2.7/ihooks.py
# line 424
print("__name__: {0}".format(globals()['__name__']))
# line 418
print("__package__: {0}".format(globals()['__package__']))
print("Begin imports")
import pickle
import io
import sys
print(dir(sys.modules[__name__]))
from . libraries.standard.player import Player
print(Player)
from . libraries.standard.player import Player # <- Line 22
#from . libraries.standard.area import Area
Yep, I'm literally trying to import Player a second time directly after the first. I'm doing that because I can see from the output that Player is imported correctly the first time. Regardless, if I use the second Player import or the commented Area import, the output is the same.
The output
*** begin interface_bridge.py ***
__name__: gameSystem.interface_bridge
__package__: gameSystem
Begin imports
Couldn't find: gameSystem.pickle
File "<iostream>", line 236, in loadGameData
File "<iostream>", line 116, in load_module
File "gameSystem.interface_bridge", line 13, in <module>
File "<iostream>", line 66, in find_module
Couldn't find: gameSystem.io
File "<iostream>", line 236, in loadGameData
File "<iostream>", line 116, in load_module
File "gameSystem.interface_bridge", line 14, in <module>
File "<iostream>", line 66, in find_module
Couldn't find: gameSystem.sys
File "<iostream>", line 236, in loadGameData
File "<iostream>", line 116, in load_module
File "gameSystem.interface_bridge", line 15, in <module>
File "<iostream>", line 66, in find_module
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', 'io', 'pickle', 'sys']
Found: gameSystem.libraries
Loaded: gameSystem.libraries
Execute: gameSystem.libraries
Found: gameSystem.libraries.standard
Loaded: gameSystem.libraries.standard
Execute: gameSystem.libraries.standard
Found: gameSystem.libraries.standard.player
Loaded: gameSystem.libraries.standard.player
Execute: gameSystem.libraries.standard.player
<class 'gameSystem.libraries.standard.player.Player'> <- Player class loaded!
Couldn't find: gameSystem.libraries.standard.libraries
File "<iostream>", line 236, in loadGameData
File "<iostream>", line 116, in load_module
File "gameSystem.interface_bridge", line 22, in <module>
File "<iostream>", line 66, in find_module
Couldn't find: gameSystem.libraries.standard.libraries
File "<iostream>", line 236, in loadGameData
File "<iostream>", line 116, in load_module
File "gameSystem.interface_bridge", line 22, in <module>
File "<iostream>", line 66, in find_module
No module named gameSystem.libraries.standard.libraries
ImportError at line 22 of gameSystem.interface_bridge: No module named gameSystem.libraries.standard.libraries
Those first three couldn't finds (pickle, io, and sys) all make sense as they are Python libraries (though it's interesting to see them being treated as if their part of the gameSystem package). My importer shouldn't handle them and kicks them back to the import system.
Then, as I interpret it, the import system starts working down the package structure (.libraries, .libraries.standard) to finally get to gameSystem.libraries.standard.player, which is loaded and executed without problem. You can see the class is ready to go as it can be printed to the output <class '...Player'>.
What happens next is where I get confused. Why is it trying to import gameSystem.libraries.standard.libraries? Why two times in a row?
I'm fairly certain this is caused by some mistake I've made in my importer but I'm baffled. I would expect both import lines to behave exactly the same way.

Related

Flutter - Dart - remove hidden character from String input

I'm trying to compare 2 strings, however my comparison always fails.
For reference, the one string is a filename I'm getting from the phones storage and it look like it ends with an apostrophe, although its not visible anywhere.
Please consider the following dart code:
import 'dart:convert';
void main() {
const Utf8Codec utf8 = Utf8Codec();
String input = 'chatnum.txt';
String stringwithapostrophe = 'chatnum.txt\'';
String compInput = utf8.encode(input).toString();
String compComp = utf8.encode(stringwithapostrophe).toString();
print (compInput);
print (compComp);
if (compInput == compComp) {
print ('Yes it matches');
} else {
print ('No it does not');
}
}
This output's a result of:
[99, 104, 97, 116, 110, 117, 109, 46, 116, 120, 116]
[99, 104, 97, 116, 110, 117, 109, 46, 116, 120, 116, 39]
No it does not
So how can I remove that last apostrophe from the String?
I've tried .removeAt and .removeLast. But I just can't crack this.
I applied regex to it. That sorted it:
String filenametosend = (basename(f.toString()))
.replaceAll(RegExp(r"[-!$%^&*()+|~=`{}##\[\]:;'’<>?,\/"
'"”'
"]"), '');
This way too:
final apostrophe = '\'';
final length = stringwithapostrophe.length;
if (length > 0 && stringwithapostrophe[length - 1] == apostrophe) {
stringwithapostrophe = stringwithapostrophe.substring(0, length - 1);
}
Or this way (remove all):
final apostrophe = '\'';
stringwithapostrophe = stringwithapostrophe.replaceAll(apostrophe, '');
Remove (any) last:
final length = stringwithapostrophe.length;
stringwithapostrophe = length > 0
? stringwithapostrophe.substring(0, length - 1)
: stringwithapostrophe;

File "<string>", line SyntaxError: unexpected EOF while parsing

I am trying to do this exercise but fail. I can only get this result:
main() File "C:\Users\chemuser\Desktop\KHL\python\chapter4_6.py", line 22, in main
asciimark = eval(line) File "<string>", line 1
^ SyntaxError: unexpected EOF while parsing
here is my fail code:
# -*- coding: cp1252 -*-
#A certain CS professor gives 5-point quizzes that are
#graded on the scale 5-A, 4-B, 3-C, 2-D, 1-F, 0-F.
#Write a program that accepts a quiz score as an
#input and prints out the corresponding grade.
import string
def main():
#set up file names
infileName = raw_input("input file name: ")
outfileName = raw_input("output file name: ")
#open files
infile = open(infileName, "r")
outfile = open(outfileName, "w")
#process each line of the input file
for line in infile.readlines():
#get the mark from line
asciimark = eval(line)
#creat grade
allgrade = "FEDCBA"
grade = allgrade[asciimark]
#write it to the output file
outfile.write(grade+"\n")
#close both files
infile.close()
outfile.close()
print "Done!"
main()
the input file is look like this (just a column of number 0 - 5, no blank line between):
5
1
0
2
3
I did a test, I add '/n' in the first line (5/n), it shows:
main() File "C:\Users\chemuser\Desktop\KHL\python\chapter4_6.py", line 22, in main
asciimark = eval(line) File "<string>", line 1
5\n
^ SyntaxError: unexpected character after line continuation character
your comments are very appreciated!!

How to handle "cannot refer to an open group at position" in Python?

With modified code taken from Python docs, in the tokenizer below I have added regular expression to match docstrings, comments and quotes which are combined into a single master regular expression and successive matches are looped over.
import collections
import re
Token = collections.namedtuple('Token', ['typ', 'value', 'line', 'column'])
def tokenize(code):
keywords = {'IF', 'THEN', 'ENDIF', 'FOR', 'NEXT', 'GOSUB', 'RETURN'}
token_specification = [
('DOC', r'([\'"]{3})[^\x00]*?\2'), # docstrings
('COMM', r'#.*'), # comments
('QUOT', r'(?:"([^"\\\\]*(?:\\.[^"\\\\]*)*)"|' # quotes
r'\'([^\'\\]*(?:\\.[^\'\\]*)*)\')|'
r'r\'([^"(]*)\((.*?)\)\4\'')
]
tok_regex = '|'.join('(?P<%s>%s)' % pair for pair in token_specification)
line_num = 1
line_start = 0
for mo in re.finditer(tok_regex, code):
kind = mo.lastgroup
value = mo.group(kind)
if kind == 'NEWLINE':
line_start = mo.end()
line_num += 1
elif kind == 'SKIP':
pass
elif kind == 'MISMATCH':
raise RuntimeError('%r unexpected on line %d' % (value, line_num))
else:
if kind == 'ID' and value in keywords:
kind = value
column = mo.start() - line_start
yield Token(kind, value, line_num, column)
statements = '''
"""docstr1
blah"""
\'''docstr2\'''
# ok
IF "okkk" and 'ole' quantity THEN
total := total + price * quantity;
tax := price * 0.05;
ENDIF;
'''
for token in tokenize(statements):
print(token)
The above gives the following error:
line 72, in <module>
for token in tokenize(statements),
line 44, in tokenize
for mo in re.finditer(tok_regex, code),
line 220, in finditer
return _compile(pattern, flags).finditer(string),
line 293, in _compile
p = sre_compile.compile(pattern, flags),
line 536, in compile
p = sre_parse.parse(p, flags),
line 829, in parse
p = _parse_sub(source, pattern, 0),
line 437, in _parse_sub
itemsappend(_parse(source, state)),
line 778, in _parse
p = _parse_sub(source, state),
line 437, in _parse_sub
itemsappend(_parse(source, state)),
line 524, in _parse
code = _escape(source, this, state),
line 415, in _escape
len(escape)) sre_constants.error: cannot refer to an open group at position 142
I understand that the problem is the fact that the QUOT regex should be matched before the other two regexes (in case this is too simplistic or plain wrong please do explain).
I am afraid that on some occasions I may end up messing the order of the regexes and not being able to get an error in order to fix it until I provide an appropriate statements.
My question is how could I be able to handle such cases/errors gracefully? Is there a try .. except approach appropriate? Any code example to illustrate this would be great.
(using Python 3.5.1)

Cannot Play After Rename File - TypeError: cannot concatenate 'str' and 'NoneType' objects

I have an error when after rename files and want to access it using the module os.system. namely the script os.system('mpg321' + ren).
I've done os.chdir on the path.
but still it is still not working.
Here I use the power mpg321 for player.
and deliberately did not use the module as pymedia or other.
Traceback (most recent call last):
File "Play_Next.py", line 52, in <module>
os.system('mpg321 ' + ren)
TypeError: cannot concatenate 'str' and 'NoneType' objects
And this my complete script..
import os, time
mypath = 'Mymusic'
filenames = os.listdir(mypath)
len_file = len(filenames)
inp = input('Enter Start Playlist (ex: 2): ')
less = len_file - inp
indexed = filenames[less]
x = os.listdir(mypath)
#pindah_dir = os.chdir(mypath)
#pindah_dir
if x[inp] in x:
print ' [+] Mp3 File indexed: ', indexed
print ' ++++++++++++++++++++++++++++++++ '
os.chdir(mypath)
while True:
try:
time.sleep(0.2)
inp += 1
print x[inp]
inden = x[inp]
rename1 = inden.replace(' ','').replace('-','').replace('(','').replace(')', '').replace("'", '')
ren = os.rename(inden, rename1)
play_next = os.system('mpg321 ' + ren)
play_next
except KeyboardInterrupt:
print "\n\tThankyou.."
break
except IndexError:
print "Repeat from the beginning.."
inp = 0
inp += 1
print x[inp]
Because that os.rename don't return the new file's path like this:
>>> import os
>>> print os.rename('/Users/zookeep/Desktop/test.py', '/Users/zookeep/Desktop/ok.py')
None
You can use os.path.join to get the rename file' name:
>>> os.path.join('/Users/zookeep/Desktop/test.py', 'ok.py')
'/Users/zookeep/Desktop/test.py/ok.py'
So change your code like:
try:
os.rename(inden, rename1)
ren = os.path.join(inden, rename1)
play_next = os.system('mpg321 ' + ren)
except Exception, error:
print error
May helps.

Parse a file in various parts

I am trying to parse a file that contains the following
# This is 1st line
# This is 2nd line
ATOM This is 3rd line
ATOM This is 4th line
# This is 5th line
# This is 6th line
I wish to use Python 2.7 to parse the file and append lines up to the line starting with ATOM to a list head_list, the lines starting with ATOM to atom_list and lines after the line containing ATOM to a tail_list.
I want to use the startswith() in Python to match lines that start with ATOM. Below is my code, i am passing counter variable which has the index of the last line in the file which starts with ATOM. yet my output does not seem to be right
#!/usr/bin/python
import sys, os
global counter
def AskForFileName () :
file_name = raw_input('Enter the name of the input file \n')
try:
if not file_name :
print "You did not enter a name !"
except :
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
return file_name
def ReadFileContents (file_name) :
#print file_name
file = open(file_name,'r')
file_strings=file.readlines()
return file_strings
def BuildHeadList(all_file_contents) :
head_list=[]
i=0
try :
for line in all_file_contents:
if line.startswith("ATOM") :
break
else :
i=int(i)+1
#print "BuildHeadList :"+str(i)+"\n"
head_list.append(line)
except :
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
return head_list
def BuildAtomList(all_file_contents) :
atom_list=[]
i=0
global counter
try :
for i,line in enumerate(all_file_contents):
if line.startswith("ATOM") :
atom_list.append(line)
counter=i
#i=int(i)+1
#print "BuildAtomList :"+str(i)+"\n"
else :
continue
except :
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
return atom_list
def BuildTailList(all_file_contents) :
tail_list=[]
i=0
global counter
counter=counter+1
print "Counter value is "+str(counter)
try :
for i,line in enumerate(all_file_contents,counter):
print i
tail_list.append(line)
except :
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
return tail_list
def WriteNewFile(head_list,atom_list,tail_list):
file=open('output.txt', 'w')
#for line in head_list :
# print>>file, line,
#for line in atom_list :
# print>>file, line,
for line in tail_list :
print>>file, line,
file.close()
file_name=AskForFileName()
all_file_contents=ReadFileContents(file_name)
head_list=BuildHeadList(all_file_contents)
atom_list=BuildAtomList(all_file_contents)
tail_list=BuildTailList(all_file_contents )
WriteNewFile(head_list,atom_list,tail_list)
This line:
enumerate(all_file_contents, counter)
Doesn't do what you think it does; it iterates over everything in the lists, but numbers them starting from counter instead of 0. A minimal fix would be:
for i, line in enumerate(all_file_contents):
if i >= counter:
tail_list.append(line)
However, much better would be to not iterate over the whole file three times. In particular, note that tail_list = all_file_contents[counter:] gets the result you want. Additionally, get rid of the global and pass counter around explicitly.
You should also read the style guide.