def decode(data, encoding=None): """Returns the unicode text from the encoded, data. Prefer encoding if given. The text is also checked for the 'coding' document variable. """ encodings = [encoding] if encoding else [] for bom, encoding in ( (codecs.BOM_UTF8, 'utf-8'), (codecs.BOM_UTF16_LE, 'utf_16_le'), (codecs.BOM_UTF16_BE, 'utf_16_be'), ): if data.startswith(bom): encodings.append(encoding) data = data[len(bom):] break else: var_coding = variables.variables(data).get("coding") if var_coding: encodings.append(var_coding) encodings.append('utf-8') encodings.append('latin1') for encoding in encodings: try: return data.decode(encoding) except (UnicodeError, LookupError): continue return data.decode('utf-8', 'replace')
def version(filename): """Returns the LilyPond version if set in the file, as a tuple of ints. First the function searches inside LilyPond syntax. Then it looks at the 'version' document variable. Then, if the document is not a LilyPond document, it simply searches for a \\version command string, possibly embedded in a comment. The version is cached until the file changes. """ mkver = lambda strings: tuple(map(int, strings)) with open(filename) as f: text = util.decode(f.read()) mode = textmode(text) tokens_ = list(ly.lex.state(mode).tokens(text)) version = ly.parse.version(tokens_) if version: return mkver(re.findall(r"\d+", version)) # look at document variables version = variables.variables(text).get("version") if version: return mkver(re.findall(r"\d+", version)) # parse whole document for non-lilypond comments if mode != "lilypond": m = re.search(r'\\version\s*"(\d+\.\d+(\.\d+)*)"', text) if m: return mkver(m.group(1).split('.'))
def decode(data, encoding=None): """Decode binary data, using encoding if specified. When the encoding can't be determined and isn't specified, it is tried to get the encoding from the document variables (see variables module). Otherwise utf-8 and finally latin1 are tried. """ enc, data = get_bom(data) for e in (enc, encoding): if e: try: return data.decode(e) except (UnicodeError, LookupError): pass latin1 = data.decode('latin1') # this never fails encoding = variables.variables(latin1).get("coding") for e in (encoding, 'utf-8'): if e and e != 'latin1': try: return data.decode(e) except (UnicodeError, LookupError): pass return latin1
def __init__(self): self.im = None self.app = QApplication(sys.argv) self.Form = QWidget() self.ui = Ui_MainWindow() self.ui.setupUi(self.Form) self.ui.b_open.clicked.connect(self.b_open_clicked) self.ui.b_filter.clicked.connect(self.b_filter_clicked) self.v = variables()
def textmode(text, guess=True): """Returns the type of the given text ('lilypond, 'html', etc.). Checks the mode variable and guesses otherwise if guess is True. """ mode = variables.variables(text).get("mode") if mode in ly.lex.modes: return mode if guess: return ly.lex.guessMode(text)
def _cached(filename): """Return a _CachedDocument instance for the filename, else creates one.""" filename = os.path.realpath(filename) try: c = _document_cache[filename] except KeyError: with open(filename, 'rb') as f: text = util.decode(f.read()) c = _document_cache[filename] = _CachedDocument() c.variables = v = variables.variables(text) c.document = ly.document.Document(text, v.get("mode")) c.filename = c.document.filename = filename return c
def encode(text, default_encoding='utf-8'): """Returns the bytes representing the text, encoded. Looks at the 'coding' variable to determine the encoding, otherwise falls back to the given default encoding, defaulting to 'utf-8'. """ encoding = variables.variables(text).get("coding") if encoding: try: return text.encode(encoding) except (LookupError, UnicodeError): pass return text.encode(default_encoding)
def __init__(self, address, port, username, password, use_https=False, log=None, *args, **kwargs): """ Initiates the ISY class. address: String of the IP address of the ISY device port: String of the port over which the ISY is serving its API username: String of the administrator username for the ISY password: String of the administrator password for the ISY use_https: [optional] Boolean of whether secured HTTP should be used log: [optional] Log file class, template in this Module """ if log is None: self.log = DummyLog() else: self.log = log self.conn = ISYhttp(self, address, port, username, password, use_https) self.configuration = configuration(self, xml=self.conn.getConfiguration()) self.nodes = nodes(self, xml=self.conn.getNodes()) self.programs = programs(self, xml=self.conn.getPrograms()) self.variables = variables(self, xml=self.conn.getVariables()) self._events = EventStream(self) if len(self.configuration) == 0: self.log.error('ISY Unable to connect.') else: if self.configuration['Weather Information']: self.climate = climate(self, xml=self.conn.getClimate()) if self.configuration['Networking Module']: self.networking = networking(self, xml=self.conn.getNetwork())
print('Disconnected from Adafruit IO!') sys.exit(1) def message(client, feed_id, payload): #print('Feed {0} received new value: {1}'.format(feed_id, payload)) payload_dict = payload_seperator(payload, seperator=',') n_sect = payload_dict['n_sect'] if payload_dict['sect1'] in ('Outlet', 'outlet', 'Outlets', 'outlets', 'Out', 'out', 'O', 'o') and (n_sect > 1): outlet_manager(payload_dict) info = relays.retrieve_info_states() #print('O1: {}, O2: {}, O3: {}, O4: {}'.format(info[0], info[1], info[2], info[3])) #--------------------------------MAIN CODE------------------------------- var = variables() ADAFRUIT_IO, PUB_FEEDS, SUB_FEEDS = var.ADAFRUIT_IO, var.PUB_FEEDS, var.SUB_FEEDS relays = four_relays(var.outlet_pins, var.outlet_names, var.outlet_types) ds = DS18B20('f', 2) num_ds = var.num_ds = ds.num_ds() client = MQTTClient(ADAFRUIT_IO['USERNAME'], ADAFRUIT_IO['KEY']) client.on_connect = connected client.on_disconnect = disconnected client.on_message = message client.connect() print('Number of DS18B20 sensors detected: {}'.format(var.num_ds)) print('Publishing a new message every 10 seconds (press Ctrl-C to quit)...') try:
def test_addString(): aInt, bInt, aString, bString, helloWorld, multiply, power, fitInto = variables( ) assert helloWorld == aString + bString
def variables(self): """A dictionary with variables defined in the text.""" return variables.variables(self.text())
from descriptive import main as descriptive from variables import main as variables from ols import main as ols import os import pandas as pd import numpy as np parser = argparse.ArgumentParser() parser.add_argument('script', nargs='?', type=str, default='all', help='Script number to run, -all- for all scripts') args = parser.parse_args() # common paths analysis_clean = '../../data/clean/analysis_clean.csv' tables_raw = '../../output/tables_raw.xlsx' if args.script == 'all' or args.script == 'variables': variables(input_path='../../data/clean/dataClean_bymonth.csv', output_path=analysis_clean) if args.script == 'all' or args.script == 'descriptive': descriptive(input_path=analysis_clean) if args.script == 'all' or args.script == 'ols': ols(input_path=analysis_clean)
def test_power(): aInt, bInt, aString, bString, helloWorld, multiply, power, fitInto = variables( ) assert power == bInt**aInt
def test_devide(): aInt, bInt, aString, bString, helloWorld, multiply, power, fitInto = variables( ) assert fitInto == bInt / aInt
def test_multiply(): aInt, bInt, aString, bString, helloWorld, multiply, power, fitInto = variables( ) assert multiply == aInt * bInt