format

How to handle portability issues in a binary file format

跟風遠走 提交于 2019-12-05 08:48:49
I'm designing a binary file format to store strings[without terminating null to save space] and binary data. i. What is the best way to deal with little/big endian systems? i.a Would converting everything to network byte order and back with ntohl()/htonl() work? ii. Will the packed structures be the same size on x86, x64 and arm? iii. Are their any inherent weakness with this approach? struct __attribute__((packed)) Header { uint8_t magic; uint8_t flags; }; struct __attribute__((packed)) Record { uint64_t length; uint32_t crc; uint16_t year; uint8_t day; uint8_t month; uint8_t hour; uint8_t

Can generators be used with string.format in python?

和自甴很熟 提交于 2019-12-05 08:30:34
"{}, {}, {}".format(*(1,2,3,4,5)) Prints: '1, 2, 3' This works, as long as the number of {} in format does not exceed the length of a tuple. I want to make it work for a tuple of arbitrary length, padding it with - s if it is of insufficient length. And to avoid making assumptions about the number of {} 's, I wanted to use a generator. Here's what I had in mind: def tup(*args): for s in itertools.chain(args, itertools.repeat('-')): yield s print "{}, {}, {}".format(*tup(1,2)) Expected: '1, 2, -' But it never returns. Can you make it work with generators? Is there a better approach? If you

String.Format or Not? [duplicate]

岁酱吖の 提交于 2019-12-05 07:54:10
This question already has answers here : Closed 10 years ago . Duplicate from : String output: format or concat in C#? Especially in C# world using String.Format for everything is really common, normally as VB.NET developer unless I have to* I don't String.Format, I prefer normal string concatenation, such as: V1 = V2 & "test-x" & V3 & "-;" to me it's better than this: V1 = String.Format("{0} test-x {1} -;", V2, V3) Am I missing something? Or is this just a personal preference? Reasons to Use String.Format (From The Answers) ( I'll try to keep this up to date ) Localization is so much easier

validating password format in Authlogic

故事扮演 提交于 2019-12-05 07:51:44
Is there a way to get Authlogic to validate the format of a password, for instance must contain at least one letter and at least one number? The omission of a validates_format_of_password_options method to be used in the acts_as_authentic config block seems to indicate that Authlogic has the opinion that one should not be imposing such a constraint on one's users. I thought I would simply put in a normal ActiveRecord validates_format_of :password , but this means that a current_user object I build is inherently invalid, as I can't retrieve the plaintext password (and wouldn't be storing it in

How to deal with time values over 24 hours in python?

…衆ロ難τιáo~ 提交于 2019-12-05 07:26:33
I'm dealing with a large amount of data that has both values and times(in strings). I am converting the string time values into datetime values with the following code: time = datetime.datetime.strptime(time, " %H:%M:%S.%f") The only problem is that some of my data has the format: 24:00:00.004. So some of the data is actually over 24 hours Python is giving me this error: ValueError: time data ' 24:00:00:004' does not match format ' %H:%M:%S.%f' Any ideas on how to deal with this problem The %H parameter can only parse values in the range 0-23. You'll have to manually deal with those specific

Get the given date format (the string specifying the format) in javascript or momentjs

久未见 提交于 2019-12-05 06:36:39
Given a datestring, how can I get the format string describing that datestring? Put another way, how can I get the format string that Date() or MomentJS (might be different for each, that's fine) would use to parse that datestring if one didn't pass an explicit format to use? So given '2016-01-01' it should output something like 'YYYY-MM-DD' , for example. (I am aware this is a simple question and may have an answer somewhere, but it is difficult to word concisely, so I could only find questions and answers about how to parse datestrings or how to display dates. None about how to output the

Formatting of DateTimeIndex in plot pandas

ぃ、小莉子 提交于 2019-12-05 06:35:00
问题 I'm having trouble deciphering the documentation for changing tick frequency and date formatting with pandas. For example: import numpy as np import pandas as pd import pandas.io.data as web import matplotlib as mpl %matplotlib inline mpl.style.use('ggplot') mpl.rcParams['figure.figsize'] = (8,6) # grab some price data px = web.DataReader('AAPL', "yahoo", '2010-12-01')['Adj Close'] px_m = px.asfreq('M', method='ffill') rets_m = px_m.pct_change() rets_m.plot(kind='bar') generates this plot:

Parsing JPEG file format: Format of entropy-coded segments (ECS)?

你离开我真会死。 提交于 2019-12-05 05:43:42
I'm having difficulty understanding the ITU-T T.81 spec for the JPEG file format. Hopefully someone else here has tried to parse JPEG files and/or knows about the details of this file format. The spec indiates that the ECS0 segment starts after the SOS segment but I can't find where in the spec it actually talks about the format of the ECS0 segment or how do detect its start. Simple JPEG implementations online are of limited help because they assume many things about the JPEGs they parse. Can anyone point me in the right direction? FYI: The JPEG file format spec is here . When the standard

Convert iso8601 string date time format to date in Java [duplicate]

怎甘沉沦 提交于 2019-12-05 05:08:41
问题 This question already has answers here : Converting ISO 8601-compliant String to java.util.Date (28 answers) Closed 3 years ago . All, I know I have asked a similar question on parsing an ISO8601 date string into a Date using Java before, but this is a more specific problem using the SimpleDateFormat class. I have read the article Wiki ISO8601 Date. I have been supplied an XML file from a customer which has a date and time with the following format: 2012-08-24T12:15:00+02:00 According to the

Format decimal as currency based on currency code

落爺英雄遲暮 提交于 2019-12-05 04:55:26
I have a table of charges with the amount, and the currency code (USD, JPY, CAD, EUR etc.), and am looking for the easiest way to properly format the currency. Using my local culture code (USA) and taking my decimal.ToString("c") gets me $0.00 output, but I'd like the correct currency sign and number of decimals based on the code. Do any libraries exist for this? I can of course write up a switch statement and custom rules for each one, but thought this must have been done before. Update: I've modified Jon B's sample code as follows static IDictionary<string, string> GetCurrencyFormatStrings()