Python - Mocking a ZipFile - python

I am trying to get further into testing with python and right now I am hard stuck at trying to write a test for the following code:
def get_files(zip_path: Path):
archive = zipfile.ZipFile(os.path.join(os.path.dirname(__file__), '..', zip_path))
python_files = []
for x in archive.filelist:
if x.filename.endswith(".py"):
python_files.append(x)
return python_files
The test I came up with looks like that:
#mock.patch('zipfile.ZipFile')
def test_get_files(mock_zipfile):
mock_zipfile.return_value.filelist.return_value = [zipfile.ZipInfo('py_file.py'), zipfile.ZipInfo('py_file.py'),
zipfile.ZipInfo('any_file.any')]
nodes = get_ast_nodes(Path('/dummy/path/archive.zip'))
assert len(nodes) == 2
But I am not able to get the test to pass nor do I have any idea what is going wrong.

If someone is looking this up, i might also add the answer. This is how I got it to work:
#mock.patch('zipfile.ZipFile')
def test_get_files(mock_zipfile):
mock_zipfile.return_value.filelist = [zipfile.ZipInfo('py_file.py'), zipfile.ZipInfo('py_file.py'),
zipfile.ZipInfo('any_file.any')]
nodes = get_python_files(zipfile.ZipFile("dummy"))
assert len(nodes) == 2

Related

Antlr4 ParserATNSimulator Debugger generate no Output

Is there a possibility to get the ParserATNSimulator Debuggingmessaging in python3 to work?
I tried to get to the core of ANTLR4 for python 3 and messed a bit with the ATN Simulation. There i wonder about the Class Attributes in ParserATNSimulator() for debugging.
class ParserATNSimulator(ATNSimulator):
debug = False
debug_list_atn_decisions = False
dfa_debug = False
retry_debug = False
Checking the code i see that this should print a lot of Information about the decision making in ANTLR4.
if self.debug or self.debug_list_atn_decisions:
print("predictATN decision " + str(dfa.decision) +
" exec LA(1)==" + self.getLookaheadName(input) +
", outerContext=" + outerContext.toString(self.parser))
Unfortunately it Producing no output when i tried to set it True. Other
parts work as expected.
Is there a possibility to get this information?
def main(argv):
istream = FileStream(argv[1])
lexer = ExLexer(istream)
stream = CommonTokenStream(lexer)
parser = ExParser(stream)
parser._interp.debug = True
parser._interp.debug_list_atn_decisions = True
parser._interp.dfa_debug = True
parser._interp.retry_debug = True
parser.stat()
Thanks in advance!
I get it with #kaby76's help to print output.
I edited the variables in the class in the path. .../site-packages/antlr/antlr4-python3-runtime/atn/ParserATNSimulator.py.

Easy way to determine a type of file (regular file, directory, symlink etc.) by path in python

I wanted an easy way to determine a type of path so I googled alot and then I wrote this:
from stat import S_ISREG, S_ISDIR, S_ISLNK
from os import stat, lstat
from os.path import isfile, islink, isdir, lexists, exists
from enum import Enum, auto
class FileTypes(Enum):
FILE = auto()
LINK_TO_FILE = auto()
DIR = auto()
LINK_TO_DIR = auto()
BROKEN_LINK = auto()
NO_SUCH = auto()
UNDEFINED = auto()
def file_type(filename):
if lexists(filename):
if isfile(filename):
if islink(filename):
return FileTypes.LINK_TO_FILE
else:
return FileTypes.FILE
else:
if isdir(filename):
if islink(filename):
return FileTypes.LINK_TO_DIR
else:
return FileTypes.DIR
else:
if islink(filename):
return FileTypes.BROKEN_LINK
else:
return FileTypes.UNDEFINED
else:
return FileTypes.NO_SUCH
Then I googled more and wrote this:
def file_type2(filename):
if lexists(filename):
if exists(filename):
mode = stat(filename).st_mode
lmode = lstat(filename).st_mode # os.lstat doesn't follow symlinks
if S_ISREG(mode) and S_ISREG(lmode):
return FileTypes.FILE
elif S_ISREG(mode) and S_ISLNK(lmode):
return FileTypes.LINK_TO_FILE
elif S_ISDIR(mode) and S_ISDIR(lmode):
return FileTypes.DIR
elif S_ISDIR(mode) and S_ISLNK(lmode):
return FileTypes.LINK_TO_DIR
else:
return FileTypes.UNDEFINED
else:
return FileTypes.BROKEN_LINK
else:
return FileTypes.NO_SUCH
Both functions do what I want, but look kinda ugly and I think that I'm missing a simpler solution hiding in some cool python lib.
Question is: Is there a better way to do this?
You can try the pathlib module which has been in stdlib since Python 3.4 (for older pythons use pip install pathlib). It defines the Path class which contains methods for both checking types of files as well as resolving symlinks. Besides, it provides a pretty convenient API:
>>> from pathlib import Path
>>> path = Path("/etc/") / "passwd"
>>> path
PosixPath('/etc/passwd')
>>> path.is_file()
True
We can make it more consise with utilizing bitmasking inside enum:
Let's assume first value describes if file exists: 0 for existing and 1 for not existing, second one will be symlink: 1 for links and 0 for non-links, third for directory: 1 if it is directory and 0 if it is not, and the last for file in hte same menner.
So if we wanted to describe file that existsand is a symlink to file, we would use 0(exists)1(link)0(non-dir)1(file)
With using meaningful values we can now consisely chain those values together with results returned from python stat wrapper.
class FileTypes(Enum):
FILE = 1 #0001
LINK_TO_FILE = 5 #0101
DIR = 2 #0010
LINK_TO_DIR = 6 #0110
BROKEN_LINK = 4 #0100
NO_SUCH = 0 #1000
UNDEFINED = #0000
def file_type(filepath):
return FileTypes.NO_SUCH if lexists(filepath) else
Filetypes(int(
str(int(islink(filepath)))
+ str(int(isdir(filepath)))
+ str(int(isfile(filepath)))))
Obviously there is issue of some illegal states like if something would report that it is both directory and file, at that point this will raise exception, it can be modified for different behaviour, but raising exception seems perfectly valid.
Also I've used pretty ugly way of adding together this value, but this is for sake of readibility. You always could do ''.join(map(str,map(int,[islink(filepath),isdir(filepath),isfile(filepath)]))) or even shorter ways

How to speed up dictionary build in Python

I have looked at the links but nothing appears to apply. I am doing what I thought would be a simple build of three dictionaries that I use elsewhere. They are not all that large but this function takes almost 4 minutes to complete. I am likely missing something and as I would like this to run faster. This is Python 3.4
class VivifiedDictionary(dict):
def __missing__(self, key):
value = self[key] = type(self)()
return value
def dict_build(exclude_chrY):
coordinate_intersection_dict = VivifiedDictionary()
aberration_list_dict = VivifiedDictionary()
gene_list_dict = VivifiedDictionary()
if eval(exclude_chrY):
chr_y = ""
else:
chr_y = "chrY"
abr_type_list = ["del", "ins"]
mouse_list = ["chr1", "chr2", "chr3", "chr4", "chr5", "chr6", "chr7", "chr8", "chr9", "chr10", "chr11", "chr12", "chr13", "chr14", "chr15", "chr16", "chr17", "chr18", "chr19", "chrX", chr_y]
for chrom in mouse_list:
for aberration in abr_type_list:
coordinate_intersection_dict[chrom][aberration] = []
aberration_list_dict[chrom][aberration] = []
gene_list_dict[chrom][aberration] = []
Pleas check this The Python Profilers I think this may help in finding the bottleneck in your bigger script.

How to instantiate an ontology using rdflib?

I have an ontology where I have defined series of classes, subclasses and properties. Now I want to automatically instantiate the ontology with Python code and save it in RDF/XML again and load it in Protege. I have written the following code:
def instantiating_ontology(rdf_address):
from rdflib import *
g = Graph()
input_RDF = g.parse(rdf_address)
#input_RDF = g.open(rdf_address, create=False)
myNamespace="http://www.semanticweb.org/.../ontologies/2015/3/RNO_V5042_RDF"
rno = Namespace(myNamespace+"#")
nodeClass = URIRef(rno+"Node")
arcClass = URIRef(rno+"Arc")
#owlNamespace = 'http://www.w3.org/2002/07/owl#NamedIndividual'
namedIndividual = URIRef('http://www.w3.org/2002/07/owl#NamedIndividual')
rdftype = URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
for i in range(0,100):
individualName = rno + "arc_"+str(arcID)
#arc_individual= BNode(individualName)
arc_individual = BNode()
#g.add()
#g.add((arc_individual,rdftype, namedIndividual))
g.add((arc_individual,rdftype, arcClass))
g.add((arc_individual,rdftype, arcClass))
#g.commit()
output_address ="RNO_V5042_RDF.owl"
g.serialize(destination = output_address)
The file contains the added triples to the rdf/xml:
<rdf:Description rdf:nodeID="N0009844208f0490887a02160fbbf8b98">
<rdf:type rdf:resource="http://www.semanticweb.org/ehsan.abdolmajidi/ontologies/2015/3/RNO_V5042#Arc"/>
but when I open the file in Protege there are no instances for the classes.
Can someone tell me if the way I defined instances is wrong or I should use different tags?
After playing around with the code and the results, I realized that the notion rdf:nodeID should be replaced with rdf:about. to do so I only needed to change
for i in range(0,100):
individualName = rno + "arc_"+str(arcID)
#arc_individual= BNode(individualName)
arc_individual = BNode() #---> remove this one
arc_individual = URIRef(individualName) #----> add this one
g.add((arc_individual,rdftype, arcClass))
g.add((arc_individual,rdftype, arcClass))
arc_individual = URIRef(individualName)
that might seem easy but took me sometime to understand. I hope this can help others. :D

create pull down from folders in nuke, then evaluate first pull down to populate second pull down inpython

I am trying to create a panel that opens on nuke start up and sets a few parameters.
What it is I want to do is have a series of pulldowns on the same panel, the items in the pulldowns will be from folders.
Problem I am having is, I would like to set the first pull down and from the choice of this pull down the second pull down reflects that choice and it menu items reflect that change and so on with each pull down, basically digging down a folder structure but each pull down result is used a variable.
I have not got very far but
import os
import nuke
import nukescripts
## define panel
pm = nuke.Panel("project Manager")
## create pulldown menus
jobPath = pm.addEnumerationPulldown( 'project', os.walk('/Volumes/Production_02/000_jobs/projects').next()[1])
seqPath = pm.addEnumerationPulldown('sequence', os.walk('/Volumes/Production_02/000_jobs/projects').next()[1])
shotPath = pm.addEnumerationPulldown('shot', os.walk('/Volumes/Production_02/000_jobs/projects').next()[1])
print jobPath
print seqPath
print shotPath
#pm.addKnob(job)
#pm.addKnob(seq)
#pm.addKnob(shot)
pm.show()
also the strings that appear in the pull downs are surounded by [' ' and so on?
cheers
-adam
You probably want to use a PythonPanel, rather than the old-style Panel, which is basically a TCL wrapper. That way, you can get callbacks when the knobs in the panel are changed.
Here's a basic example:
import os
import nuke
import nukescripts.panels
class ProjectManager(nukescripts.panels.PythonPanel):
def __init__(self, rootDir='/Volumes/Production_02/000_jobs/projects'):
super(ProjectManager, self).__init__('ProjectManager', 'id.ProjectManager')
self.rootDir = rootDir
self.project = self.sequence = self.shot = None
projectDirs = [x for x in os.listdir(rootDir)
if os.path.isdir(os.path.join(rootDir, x))]
self.project = projectDirs[0]
self.projectEnum = nuke.Enumeration_Knob('project', 'project', projectDirs)
self.addKnob(self.projectEnum)
self.seqEnum = nuke.Enumeration_Knob('sequence', 'sequence', [])
self.addKnob(self.seqEnum)
self.shotEnum = nuke.Enumeration_Knob('shot', 'shot', [])
self.addKnob(self.shotEnum)
self._projectChanged()
def _projectChanged(self):
self.project = self.projectEnum.value()
projectDir = os.path.join(self.rootDir, self.project)
projectSeqDirs = [x for x in os.listdir(projectDir)
if os.path.isdir(os.path.join(projectDir, x))]
self.seqEnum.setValues(projectSeqDirs)
self._sequenceChanged()
def _sequenceChanged(self):
s = self.seqEnum.value()
if s:
self.sequence = s
seqDir = os.path.join(self.rootDir, self.project, s)
seqShotDirs = [x for x in os.listdir(seqDir)
if os.path.isdir(os.path.join(seqDir, x))]
else:
self.sequence = None
seqShotDirs = []
self.shotEnum.setValues(seqShotDirs)
self._shotChanged()
def knobChanged(self, knob):
if knob is self.projectEnum:
self._projectChanged()
elif knob is self.seqEnum:
self._sequenceChanged()
elif knob is self.shotEnum:
self.shot = self.shotEnum.value()
p = ProjectManager()
if p.showModalDialog():
print p.project, p.sequence, p.shot
Note that this example is only to demonstrate the basic design of a PythonPanel subclass. It has a few small logic issues (in the context of Nuke), and is written to be as clear as possible, rather than as efficient or idiomatic as possible.
Anyway, hopefully this gives you an idea of how to go about building what you're after.

Categories

Resources