What is the linux equivalent of sys.getwindowsversion() - python

There is no sys.getlinuxversion(), and I get the feeling I may have to piece it together from a few places (which is fine); but, maybe not?
import sys, os
from collections import namedtuple
## This defines an interface similar to the object that getwindowsversion() returns
## (I say 'similar' so I don't have to explain the descriptor magic behind it)
WindowsVersion = namedtuple('WindowsVersion', 'major minor build platform product_type service_pack service_pack_major service_pack_minor suite_mask')
## Looking for something that quacks like this:
LinuxVersion = namedtuple('LinuxVersion', 'major minor build whatever attrs are needed')
## To create something that quacks similarly to this (in case you're wondering)
PlatformVersion = namedtuple('PlatformVersion', 'major minor micro build info')
## EDIT: A newer version of Version, perhaps?
class Version:
_fields = None
def __init__(self, *subversions, **info)
self.version = '.'.join([str(sub) for sub in subversions])
self.info = info
instance_fields = self.__class__._fields
if instance_fields is not None:
if isinstance(instance_fields, str):
instance_fields = instance_fields.split()
for field in instance_fields:
if field in self.info:
setattr(self, field, self.info.pop(field))
## This makes the PlatformVersion (or any other Version) definition a mere:
class PlatformVersion(Version):
_fields = 'build' ## :)
## Now PlatformInfo looks something like:
class PlatformInfo:
def __init__(self, *version_number, platform=None, version_info=None, **info):
self.platform = sys.platform if platform is None else platform
self.info = info
if self.platform in ('win32', 'win64'):
works_great = sys.getwindowsversion()
self.version = PlatformVersion(works_great.major, works_great.minor, works_great.service_pack_major, build=works_great.build, **dict(version_info))
else:
self.version = your_answer(PlatformVersion(os.uname().release, build=os.uname().version, **dict(version_info))), 'hopefully'
self.version = self.version[0] ## lol
Thanks!

Linux (and all Unix-like systems) have uname syscall that provide such information:
>>> import os
>>> os.uname().release
'3.11.10-17-desktop'
It shows version of kernel.
Note that on Python 2 it will return tuple instead of namedtuple:
>>> import os
>>> os.uname()[2]
'3.11.10-17-desktop'

Related

Confluent Kafka python schema parser causes conflict with fastavro

I am running Python 3.9 with Confluent Kafka 1.7.0, avro-python3 1.10.0 and fastavro 1.4.1.
The following code uses Avro schema encoder in order to encode a message, which succeeds only if we transform the resulting schema encoding by getting rid of the MappingProxyType:
from confluent_kafka import Producer
from confluent_kafka.avro import CachedSchemaRegistryClient, MessageSerializer
from fastavro.schema import parse_schema
from fastavro.validation import validate
from types import MappingProxyType
from typing import Any
import sys
def transformMap(item: Any) -> Any:
if type(item) in {dict, MappingProxyType}:
return {k:transformMap(v) for k,v in item.items()}
elif type(item) is list:
return [transformMap(v) for v in item]
else:
return item
def main(argv = None):
msgType = 'InstrumentIdMsg'
idFigi = 'BBG123456789'
head = {'sDateTime': 1, 'msgType': msgType, 'srcSeq': 1,
'rDateTime': 1, 'src': 'Brownstone', 'reqID': None,
'sequence': 1}
msgMap = {'head': head, 'product': 'Port', 'idIsin': None, 'idFigi': idFigi,
'idBB': None, 'benchmark': None, 'idCusip': None,'idCins': None}
registryClient = CachedSchemaRegistryClient(url = 'http://local.KafkaRegistry.com:8081')
schemaId, schema, version = registryClient.get_latest_schema(msgType)
serializer = MessageSerializer(registry_client = registryClient)
schemaMap = schema.to_json()
# NOTE:
# schemaMap cannot be used since it uses mappingproxy
# which causes validate() and parse_schema() to throw
schemaDict = transformMap(schemaMap)
isValid = validate(datum = msgMap, schema = schemaDict, raise_errors = True)
parsed_schema = parse_schema(schema = schemaDict)
msg = serializer.encode_record_with_schema_id(schema_id = schemaId,
record = msgMap)
producer = Producer({'bootstrap.servers': 'kafkaServer:9092'})
producer.produce(key = idFigi,
topic = 'TOPIC_NAME',
value = msg)
return 0
if __name__ == '__main__':
sys.exit(main())
The transformation basically leaves everything unchanged except altering MappingProxyType to dict instances.
Is there a problem in the way I am calling the standard library which causes mapping proxy to be used, which in turn causes fastavro to throw? Can this be fixed by something as a user, or is this really a bug in the Confluent Kafka library?
In addition, the output schemaId from registryClient.get_latest_schema() is marked in the docs to return str but returns int. If I understand correctly, this is the intended input into the schema_id parameter of serializer.encode_record_with_schema_id() (and it works correctly if I call it), which is also marked as int. Is that a typo in the docs? In other words, it seems either registryClient.get_latest_schema() should return an integer, or serializer.encode_record_with_schema_id() should take a string, or I am doing something incorrectly :) Which one is it?
Thank you very much.

Add metadata to TestCase in Python's unittest

I'd like to add metadata to individual tests in a TestCase that I've written to use Python's unittest framework. The metadata (a string, really) needs to be carried through the testing process and output to an XML file.
Other than remaining with the test the data isn't going to be used by unittest, nor my test code. (I've got a program that will run afterwards, open the XML file, and go looking for the metadata/string).
I've previously used NUnit which allows one to use C# attribute to do this. Specifically, you can put this above a class:
[Property("SmartArrayAOD", -3)]
and then later find that in the XML output.
Is it possible to attach metadata to a test in Python's unittest?
Simple way for just dumping XML
If all you want to do is write stuff to an XML file after every unit test, just add a tearDown method to your test class (e.g. if you have , give it a).
class MyTest(unittest.TestCase):
def tearDown(self):
dump_xml_however_you_do()
def test_whatever(self):
pass
General method
If you want a general way to collect and track metadata from all your tests and return it at the end, try creating an astropy table in your test class's __init__() and adding rows to it during tearDown(), then extracting a reference to your initialized instances of your test class from unittest, like this:
Step 1: set up a re-usable subclass of unittest.TestCase so we don't have to duplicate the table handling
(put all the example code in the same file or copy the imports)
"""
Demonstration of adding and retrieving meta data from python unittest tests
"""
import sys
import warnings
import unittest
import copy
import time
import astropy
import astropy.table
if sys.version_info < (3, 0):
from StringIO import StringIO
else:
from io import StringIO
class DemoTest(unittest.TestCase):
"""
Demonstrates setup of an astropy table in __init__, adding data to the table in tearDown
"""
def __init__(self, *args, **kwargs):
super(DemoTest, self).__init__(*args, **kwargs)
# Storing results in a list made it convenient to aggregate them later
self.results_tables = [astropy.table.Table(
names=('Name', 'Result', 'Time', 'Notes'),
dtype=('S50', 'S30', 'f8', 'S50'),
)]
self.results_tables[0]['Time'].unit = 'ms'
self.results_tables[0]['Time'].format = '0.3e'
self.test_timing_t0 = 0
self.test_timing_t1 = 0
def setUp(self):
self.test_timing_t0 = time.time()
def tearDown(self):
test_name = '.'.join(self.id().split('.')[-2:])
self.test_timing_t1 = time.time()
dt = self.test_timing_t1 - self.test_timing_t0
# Check for errors/failures in order to get state & description. https://stackoverflow.com/a/39606065/6605826
if hasattr(self, '_outcome'): # Python 3.4+
result = self.defaultTestResult() # these 2 methods have no side effects
self._feedErrorsToResult(result, self._outcome.errors)
problem = result.errors or result.failures
state = not problem
if result.errors:
exc_note = result.errors[0][1].split('\n')[-2]
elif result.failures:
exc_note = result.failures[0][1].split('\n')[-2]
else:
exc_note = ''
else: # Python 3.2 - 3.3 or 3.0 - 3.1 and 2.7
# result = getattr(self, '_outcomeForDoCleanups', self._resultForDoCleanups) # DOESN'T WORK RELIABLY
# This is probably only good for python 2.x, meaning python 3.0, 3.1, 3.2, 3.3 are not supported.
exc_type, exc_value, exc_traceback = sys.exc_info()
state = exc_type is None
exc_note = '' if exc_value is None else '{}: {}'.format(exc_type.__name__, exc_value)
# Add a row to the results table
self.results_tables[0].add_row()
self.results_tables[0][-1]['Time'] = dt*1000 # Convert to ms
self.results_tables[0][-1]['Result'] = 'pass' if state else 'FAIL'
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=astropy.table.StringTruncateWarning)
self.results_tables[0][-1]['Name'] = test_name
self.results_tables[0][-1]['Notes'] = exc_note
Step 2: set up a test manager that extracts metadata
def manage_tests(tests):
"""
Function for running tests and extracting meta data
:param tests: list of classes sub-classed from DemoTest
:return: (TextTestResult, Table, string)
result returned by unittest
astropy table
string: formatted version of the table
"""
table_sorting_columns = ['Result', 'Time']
# Build test suite
suite_list = []
for test in tests:
suite_list.append(unittest.TestLoader().loadTestsFromTestCase(test))
combo_suite = unittest.TestSuite(suite_list)
# Run tests
results = [unittest.TextTestRunner(verbosity=1, stream=StringIO(), failfast=False).run(combo_suite)]
# Catch test classes
suite_tests = []
for suite in suite_list:
suite_tests += suite._tests
# Collect results tables
results_tables = []
for suite_test in suite_tests:
if getattr(suite_test, 'results_tables', [None])[0] is not None:
results_tables += copy.copy(suite_test.results_tables)
# Process tables, if any
if len(results_tables):
a = []
while (len(a) == 0) and len(results_tables):
a = results_tables.pop(0) # Skip empty tables, if any
results_table = a
for rt in results_tables:
if len(rt):
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=DeprecationWarning)
results_table = astropy.table.join(results_table, rt, join_type='outer')
try:
results_table = results_table.group_by(table_sorting_columns)
except Exception:
print('Error sorting test results table. Columns may not be in the preferred order.')
column_names = list(results_table.columns.keys())
alignments = ['<' if cn == 'Notes' else '>' for cn in column_names]
if len(results_table):
rtf = '\n'.join(results_table.pformat(align=alignments, max_width=-1))
exp_res = sum([result.testsRun - len(result.skipped) for result in results])
if len(results_table) != exp_res:
print('ERROR forming results table. Expected {} results, but table length is {}.'.format(
exp_res, len(results_table),
))
else:
rtf = None
else:
results_table = rtf = None
return results, results_table, rtf
Step 3: Example usage
class FunTest1(DemoTest):
#staticmethod
def test_pass_1():
pass
#staticmethod
def test_fail_1():
assert False, 'Meant to fail for demo 1'
class FunTest2(DemoTest):
#staticmethod
def test_pass_2():
pass
#staticmethod
def test_fail_2():
assert False, 'Meant to fail for demo 2'
res, tab, form = manage_tests([FunTest1, FunTest2])
print(form)
print('')
for r in res:
print(r)
for error in r.errors:
print(error[0])
print(error[1])
Sample results:
$ python unittest_metadata.py
Name Result Time Notes
ms
-------------------- ------ --------- ----------------------------------------
FunTest2.test_fail_2 FAIL 5.412e-02 AssertionError: Meant to fail for demo 2
FunTest1.test_fail_1 FAIL 1.118e-01 AssertionError: Meant to fail for demo 1
FunTest2.test_pass_2 pass 6.199e-03
FunTest1.test_pass_1 pass 6.914e-03
<unittest.runner.TextTestResult run=4 errors=0 failures=2>
Should work with python 2.7 or 3.7. You can add whatever columns you want to the table. You can add parameters and stuff to the table in setUp, tearDown, or even during the tests.
Warnings:
This solution accesses a protected attribute _tests of unittest.suite.TestSuite, which can have unexpected results. This specific implementation works as expected for me in python2.7 and python3.7, but slight variations on how the suite is built and interrogated can easily lead to strange things happening. I couldn't figure out a different way to extract references to the instances of my classes that unittest uses, though.

fixing old_version on yowsup

I have been tring to register number on Yowsup, but I got old_version error.
Here is my env_s40.py file:
from .env import YowsupEnv
import hashlib
class S40YowsupEnv(YowsupEnv):
_VERSION = "2.16.11"
_OS_NAME= "S40"
_OS_VERSION = "14.26"
_DEVICE_NAME = "302"
_MANUFACTURER = "Nokia"
_TOKEN_STRING = "PdA2DJyKoUrwLw1Bg6EIhzh502dF9noR9uFCllGk1478194306452"
_AXOLOTL = True
def getVersion(self):
return self.__class__._VERSION
def getOSName(self):
return self.__class__._OS_NAME
def getOSVersion(self):
return self.__class__._OS_VERSION
def getDeviceName(self):
return self.__class__._DEVICE_NAME
def getManufacturer(self):
return self.__class__._MANUFACTURER
def isAxolotlEnabled(self):
return self.__class__._AXOLOTL
def getToken(self, phoneNumber):
return hashlib.md5(self.__class__._TOKEN_STRING.format(phone = phoneNumber).encode()).hexdigest()
def getUserAgent(self):
return self.__class__._USERAGENT_STRING.format(
WHATSAPP_VERSION = self.getVersion(),
OS_NAME = self.getOSName() + "Version",
OS_VERSION = self.getOSVersion(),
DEVICE_NAME = self.getDeviceName(),
MANUFACTURER = self.getManufacturer()
)
And I got
DEBUG:yowsup.env.env:Current env changed to s40
yowsup-cli v2.0.15
yowsup v2.5.0
Copyright (c) 2012-2016 Tarek Galal
http://www.openwhatsapp.org
This software is provided free of charge. Copying and redistribution is
encouraged.
If you appreciate this software and you would like to support future
development please consider donating:
http://openwhatsapp.org/yowsup/donate
DEBUG:yowsup.common.http.warequest:{'Accept': 'text/json', 'User-Agent': 'WhatsApp/2.16.7 S40Version/14.26 Device/Nokia-302'}
DEBUG:yowsup.common.http.warequest:cc=255&in=716343889&lc=GB&lg=en&sim_mcc=640&sim_mnc=002&mcc=640&mnc=002&method=sms&mistyped=6&network_radio_type=1&simnum=1&s=&copiedrc=1&hasinrc=1&rcmatch=1&pid=6444&rchash=5b9d791a39befe77a165a669cac86d3fa12c7390536885aa43555059748747e9&anhash=Q%90%1D%2Am%BA%EF%1C%8E%E1%84%94%E4%93%C6%DD%AB%B55E&extexist=1&extstate=1&token=fe234b378d154c6dcca365d264ed96cf&id=%A0%3B%2B%C3%B6%A2%F9%04%CB%FA%AE%887%7FY%F7E%E6%D3%17
DEBUG:yowsup.common.http.warequest:Opening connection to v.whatsapp.net
DEBUG:yowsup.common.http.warequest:Sending GET request to /v2/code?cc=255&in=716343889&lc=GB&lg=en&sim_mcc=640&sim_mnc=002&mcc=640&mnc=002&method=sms&mistyped=6&network_radio_type=1&simnum=1&s=&copiedrc=1&hasinrc=1&rcmatch=1&pid=6444&rchash=5b9d791a39befe77a165a669cac86d3fa12c7390536885aa43555059748747e9&anhash=Q%90%1D%2Am%BA%EF%1C%8E%E1%84%94%E4%93%C6%DD%AB%B55E&extexist=1&extstate=1&token=fe234b378d154c6dcca365d264ed96cf&id=%A0%3B%2B%C3%B6%A2%F9%04%CB%FA%AE%887%7FY%F7E%E6%D3%17
INFO:yowsup.common.http.warequest:{"login":"255716MYNUMBER","status":"fail","reason":"old_version"}
status: fail
reason: old_version
login: 255716MYNUMBER
How can I fix that?
Probably a bad question for so.
But a short google gave:
https://github.com/tgalal/yowsup/issues/1861#issuecomment-264792842
There are also other discussions regarding a similar issue.
in the env_android file change the version to the most current one (whatsapp):

How to use the Typed unmarshaller in suds?

I have existing code that processes the output from suds.client.Client(...).service.GetFoo(). Now that part of the flow has changed and we are no longer using SOAP, instead receiving the same XML through other channels. I would like to re-use the existing code by using the suds Typed unmarshaller, but so far have not been successful.
I came 90% of the way using the Basic unmarshaller:
tree = suds.umx.basic.Basic().process(xmlroot)
This gives me the nice tree of objects with attributes, so that the pre-existing code can access tree[some_index].someAttribute, but the value will of course always be a string, rather than an integer or date or whatever, so the code can still not be re-used as-is.
The original class:
class SomeService(object):
def __init__(self):
self.soap_client = Client(some_wsdl_url)
def GetStuff(self):
return self.soap_client.service.GetStuff()
The drop-in replacement that almost works:
class SomeSourceUntyped(object):
def __init__(self):
self.url = some_url
def GetStuff(self):
xmlfile = urllib2.urlopen(self.url)
xmlroot = suds.sax.parser.Parser().parse(xmlfile)
if xmlroot:
# because the parser creates a document root above the document root
tree = suds.umx.basic.Basic().process(xmlroot)[0]
else:
tree = None
return tree
My vain effort to understand suds.umx.typed.Typed():
class SomeSourceTyped(object):
def __init__(self):
self.url = some_url
self.schema_file_name =
os.path.realpath(os.path.join(os.path.dirname(__file__),'schema.xsd'))
with open(self.schema_file_name) as f:
self.schema_node = suds.sax.parser.Parser().parse(f)
self.schema = suds.xsd.schema.Schema(self.schema_node, "", suds.options.Options())
self.schema_query = suds.xsd.query.ElementQuery(('http://example.com/namespace/','Stuff'))
self.xmltype = self.schema_query.execute(self.schema)
def GetStuff(self):
xmlfile = urllib2.urlopen(self.url)
xmlroot = suds.sax.parser.Parser().parse(xmlfile)
if xmlroot:
unmarshaller = suds.umx.typed.Typed(self.schema)
# I'm still running into an exception, so obviously something is missing:
# " Exception: (document, None, ), must be qref "
# Do I need to call the Parser differently?
tree = unmarshaller.process(xmlroot, self.xmltype)[0]
else:
tree = None
return tree
This is an obscure one.
Bonus caveat: Of course I am in a legacy system that uses suds 0.3.9.
EDIT: further evolution on the code, found how to create SchemaObjects.

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