How do I use the Address formats in i18napis? - reactjs

I'm am trying to use the i18napis.appspot.com/address address formats for a React application so that I can create localized address forms, but it's not clear to me how to use it.
For example, this is the info for Germany:
{
zipex: "26133,53225",
key: "DE",
zip: "\d{5}",
fmt: "%N%n%O%n%A%n%Z %C",
id: "data/DE",
posturl: "http://www.postdirekt.de/plzserver/",
require: "ACZ",
name: "GERMANY"
}
The zip is a pretty straight forward regex, but how do I use the fmt: "%N%n%O%n%A%n%Z %C" format? What are N O A Z and C in this context?

According to Google's libaddressinput documentation,
N - Name
O - Organization
A - Street Address Line(s)
Z - Zip or postal code
C - City or Locality

Related

How to validate user input of currency

So I currently developing a website that support many languages. I have an input box where user can input the amount of currency inside. I need a function to validate if that input is legit or not.
however, because different countries use different format of number.
for example: England use '.' for decimal and ',' for thousand separator .
Where as Germany use ',' for decimal and '.' for thousand separator.
French use ',' for decimal and (space) for thousand separator...
And for Chinese/Jap , they even dont use number "1-9" to describe number
I can make a very big if-else function to do the validate base on the language they are using. something like this
number = userinput()
if "de":
return deValidator(number)
if "fr":
return frValidator(number)
if "en":
return enValidator(number)
if "zh":
return zhValidator(number)
However, is there any wiser way to do it?? what I am looking for is something like a already-built validator/library or an easier approach to solve this problem without having to writing different validator for different language
You can leverage on toLocaleString() method to help to build a validator; The toLocaleString() method returns a string with a language sensitive representation of the number.
const number = 123456.789;
// German uses comma as decimal separator and period for thousands
console.log(number.toLocaleString('de-DE'));
// → 123.456,789
// Arabic in most Arabic speaking countries uses Eastern Arabic digits
console.log(number.toLocaleString('ar-EG'));
// → ١٢٣٤٥٦٫٧٨٩
// India uses thousands/lakh/crore separators
console.log(number.toLocaleString('en-IN'));
// → 1,23,456.789
// the nu extension key requests a numbering system, e.g. Chinese decimal
console.log(number.toLocaleString('zh-Hans-CN-u-nu-hanidec'));
// → 一二三,四五六.七八九
// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
console.log(number.toLocaleString(['ban', 'id']));
// → 123.456,789
With this method, you can also format numbers with currency information:
const number = 10000000;
number.toLocaleString('it-IT', {style: 'currency', currency: 'EUR'})
// → 10.000.000,00 €
number.toLocaleString('it-IT', {style: 'currency', currency: 'USD'})
// → 10.000.000,00 US$
number.toLocaleString('en-US', {style: 'currency', currency: 'EUR'})
// → €10,000,000.00
number.toLocaleString('en-US', {style: 'currency', currency: 'USD'})
// → $10,000,000.00
For more details: toLocaleString https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

read values as string of yaml with libyaml

I'm need simple example for using libyaml to get all keys+values as strings from yaml file.
lets say i have this yaml file:
Active Groups:
btcoex: yes
datapath: no
Header:
TLV_data_version: 1
command id: 0xf1
group id: 0x01
btcoex:
enabel ATS:
Active: yes
Apply_point: APPLY_POINT_AFTER_ALIVE
selection:
set: 1
table: 3
cpu: 1
how can I just print all the keys and values?

How can I read this line in the C language

I am writing in C and I am trying to read this line:
phillip.allen#enron.com -> tim.belden#enron.com at 989883540
I want 4 separate strings:
sender_username: phillip.allen
sender_hostname: enron.com
receiver_username: tim.belden
receiver_hostname: enron.c
I want to get rid of the "at 989883540" part of the text.
I am using this conversion:
"%49[^# ]#%49s -> %49[^# ]#%49s"
I seem to get the sender username and hostname, so the first part of the email before the -> symbol, but I cannot read the receiver part of tim.belden part.
Replacing %49s with %49[^ ] should do the trick:
"%49[^#]#%49[^ ] -> %49[^#]#%49[^ ]"
Here is a demo on ideone.

Output formatting in Python: replacing several %s with the same variable

I'm trying to maintain/update/rewrite/fix a bit of Python that looks a bit like this:
variable = """My name is %s and it has been %s since I was born.
My parents decided to call me %s because they thought %s was a nice name.
%s is the same as %s.""" % (name, name, name, name, name, name)
There are little snippets that look like this all over the script, and I was wondering whether there's a simpler (more Pythonic?) way to write this code. I've found one instance of this that replaces the same variable about 30 times, and it just feels ugly.
Is the only way around the (in my opinion) ugliness to split it up into lots of little bits?
variable = """My name is %s and it has been %s since I was born.""" % (name, name)
variable += """My parents decided to call me %s because they thought %s was a nice name.""" % (name, name)
variable += """%s is the same as %s.""" % (name, name)
Use a dictionary instead.
var = '%(foo)s %(foo)s %(foo)s' % { 'foo': 'look_at_me_three_times' }
Or format with explicit numbering.
var = '{0} {0} {0}'.format('look_at_meeee')
Well, or format with named parameters.
var = '{foo} {foo} {foo}'.format(foo = 'python you so crazy')
Python 3.6 has introduced a simpler way to format strings. You can get details about it in PEP 498
>>> name = "Sam"
>>> age = 30
>>> f"Hello, {name}. You are {age}."
'Hello, Sam. You are 30.'
It also support runtime evaluation
>>>f"{2 * 30}"
'60'
It supports dictionary operation too
>>> comedian = {'name': 'Tom', 'age': 30}
>>> f"The comedian is {comedian['name']}, aged {comedian['age']}."
The comedian is Tom, aged 30.
Use formatting strings:
>>> variable = """My name is {name} and it has been {name} since..."""
>>> n = "alex"
>>>
>>> variable.format(name=n)
'My name is alex and it has been alex since...'
The text within the {} can be a descriptor or an index value.
Another fancy trick is to use a dictionary to define multiple variables in combination with the ** operator.
>>> values = {"name": "alex", "color": "red"}
>>> """My name is {name} and my favorite color is {color}""".format(**values)
'My name is alex and my favorite color is red'
>>>
Use the new string.format:
name = 'Alex'
variable = """My name is {0} and it has been {0} since I was born.
My parents decided to call me {0} because they thought {0} was a nice name.
{0} is the same as {0}.""".format(name)
>>> "%(name)s %(name)s hello!" % dict(name='foo')
'foo foo hello!'
You could use named parameters. See examples here
variable = """My name is {0} and it has been {0} since I was born.
My parents decided to call me {0} because they thought {0} was a nice name.
{0} is the same as {0}.""".format(name)
have a look at Template Strings
If you are using Python 3, than you can also leverage, f-strings
myname = "Test"
sample_string = "Hi my name is {name}".format(name=myname)
to
sample_string = f"Hi my name is {myname}"

How to receive variables in a post in google app engine that contains string with chars like: õ á?

email = self.request.get('email')
name = self.request.get('name')
mail.send_mail(sender="myemail", email=email, body=name, subject="sss " + name + "sdafsaã")
// added ã: the problem was that "sdafsaã" should be u"sdafsaã". with a "u" before the string. and now it works
then i get this
main.py", line 85, in post
subject="sss " + name + "sdafsa",
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 36: ordinal not in range(128)
the might have chars like õ ó and something like that.
for more details:
the code to run the worker(the code before)
the name is the one that is received from the datastore and contains chars like õ and ó...
taskqueue.add(url='/emailworker', params={'email': e.email, 'name': e.name})
thanks
Try reading a little about how unicode works in Python:
Dive Into Python - Unicode
Unicode In Python, Completely Demystified
Also, make sure you're running Python 2.5 if you are seeing this error on the development server.
You should use:
email = self.request.get('email')
name = self.request.get('name')
mail.send_mail(sender="myemail",
email=email,
body=name,
subject="hello " + name.encode('utf-8') + " user!")
The variable name is a unicode string and should encoded in utf-8 or in the kind of encode you are using in you web application before concatenating to other byte strings.
Without name.encode(), Python uses the default 7 bits ascii codec that can't encode that specific character.
the problem is joining 2 strings: ||| body = name + "ã" => error ||| body = name + u"ã" => works!!! |||
Try with encode
t ='việt ứng '
m = MyModel()
m.data = t.encode('utf-8')
m.put() #success!

Resources