Posts

arrays - Converting String of Zero's and One's to binary byte Python -

i have code using convert file bytes bytes strings of 0's , 1's. with open(file, mode='rb') file: content = file.read() b = bytearray(content) number in b: data = bin(number)[2:].zfill(8) print data i output 10110100, 00000001, etc, etc should be. i want take strings of bits in output , convert them bytes, write bytes new file. i have tried following code: list = ('11111111', '11111110', '01101000', '00000000', '01101001', '00000000', '00001101', '00000000', '00001010', '00000000') def writer(stuff): blob = struct.pack('!h', int(stuff, 2)) open('test.txt', "a+b") file: file.write(blob) strings in list: writer(strings) the original text file contains word "hi" , all, when write new file output "▒▒hi"

ruby on rails - How to validate the presence of attributes when updating a record? -

i new rails , try find validation method corresponding validate presence of attribute when updating record. if attribute not present, meaning attribute not exist request body, rails should not update record. validates :description, presence: true and validates_presence_of :description doesn't seem job. there method purpose? seems quite common in every day scenarios. if say: model.update(hash_that_has_no_description_key) then you're not touching :description : sending hash without :description key update not same sending in hash :description => nil . if model valid (i.e. has description) update won't invalidate because won't touch :description . you this: if attribute not present, meaning attribute not exist request body, rails should not update record. since you're talking request body (which models shouldn't know about) should dealing logic in controller prepares incoming data update call. you check in controller , ...

asp.net - Regex for Range $5,000 - $1,000,000 (with commas and dollar sign optional) -

i need creating regex number range of 4,000-1,000,000 commas , dollar sign being optional user. i'm trying annotate validation departments budget using asp.net regular expression or custom validator permits optional dollar sign , commas range $5000.00 $1,000,000 ^\$?([5-9][0-9]{3,5}|1000000)$ sadly, didn't work it's came with, appreciated! is solid choice, if notice ending more simple way being of second 1 down below ,? make shorter in length. ^(?:5,?\d{3}|[6-9],?\d{3}|[1-9]\d{1,2},?\d{3}|1,000,000|1000000)$ vs ^\$?([5-9],?\d{3}|\d{2,3}?,?\d{3}|(?:1,?000,?000))$ the second 1 lot cleaner , can tell works fully.

Python: How to convert a large zipped csv file to a dictionary? -

i had following code working convert csv file dictionary. with open(filename, encoding='utf-8') file: dictreader = csv.dictreader(file) row in dictreader: #do work these csv files arrive zipped , figured i'd attempt modify code read zipped csv files dictionary. things tricky since understand it, when opening file inside zip archive, file opened bytes rather text. had working reading entire file in, , converting text, hit large file , had memory error. updated code read file in 1 line @ time , suspect i'll have build dictionary manually, figured worth asking here. thanks pmsh.93, have working. with zipfile.zipfile(filename) zipfile: fname in zipfile.infolist(): zipfile.open(fname) file: file = io.textiowrapper(file, encoding="utf-8") row in csv.dictreader(file): #do work

jquery - movehover from Selenium on a dropdown -

below code in html <div id="pmenu_root_10" class="itemborder" style="position: absolute; left: 809px; top: 0px; width: 58px; height: 98px; z-index: 1000; background: rgb(190, 213, 231) none repeat scroll 0% 0%; cursor: default;" onmouseover="pmenu.over('root',10)" onmouseout="pmenu.out('root',10)" onclick="pmenu.click('root',10)"> <div class="lowtext" style="font-size: 11px; font-family: arial; color: #0066cc; position: absolute; left: 1px; top: 1px; width: 56px; height: 96px">reports</div> </div> i trying hover mouse selenium can select 1 of sub menu present. selenium not able element webelement ele = driver.findelement(by.xpath(".//div[@id='pmenu_root_10']/div[text()='reports']")); actions act = new actions(driver); act.movetoelement(ele).perform(); i tried using abosulte xpath given firepath .//*@id='login...

ios - uipageviewcontroller disable right swipe -

i have uipageviewcontroller 4 individual view controllers in it. want disable right swipe action first view controller , disable left swipe action fourth view controller. when app launches, want user able swipe left , not right, because swiping right, takes user fourth view controller , swiping left on fourth view controller takes user first view controller. here code. class tutorialpageviewcontroller: uipageviewcontroller, uipageviewcontrollerdatasource, uipageviewcontrollerdelegate { weak var tutorialdelegate: tutorialpageviewcontrollerdelegate? lazy var vcarr: [uiviewcontroller] = { return [self.vcinstance(name: "firstvc"), self.vcinstance(name: "secondvc"), self.vcinstance(name: "thirdvc"), self.vcinstance(name: "fourthvc")] }() private func vcinstance(name: string) -> uiviewcontroller { return uistoryboard(name: "main", bundle: nil).instanti...

Trouble with parsing and "doing math" with numbers (A simple java calculator) -

i attempting take simple string statement , parse print out simple answer. so far, cannot figure out why consistently getting wrong answers. say example - plug in string "2 * 3 * 4 * 5 / 2 / 3 / 2". the expected answer 10, receive answer of 1.5 can see issue here? assume not case of order of operations (i have not got far yet). public class testingforexcel { static string tableholder = "2 * 3 * 4 * 5 / 2 / 3 / 2"; public static void main(string args[]){ string[] fracequationholder = tableholder.split(" ",tableholder.length()); // holds fractions , operator string operators = ""; double operand; double operand2; double answer = 0; for(int =0; <= (fracequationholder.length-2); i+=2){ operators = fracequationholder[i+1]; operand = double.parsedouble(fracequationholder[i]); operand2 = double.parsedouble(fracequationholder[i+2]); i...