I am looking to format a phone number with twig.
Basically, I wanted to separate every 2 digits with this function and it works perfectly when I have a 10-digit phone number like 0600000000:
{% set splitPhone = store.phones['phone_store']|split('', 2) %}
{% set phone = splitPhone|join(' ') %}
I get what I want:
06 00 00 00 00
The problem is that my data can also arrive in the form: +336000000
so in that case I get
+3 36 00 00 00
I would like something like : + 336 00 00 00
In this case, I would like to find a general technique to ensure that if my number is 10 digits, it does as I currently do but in case there is the "+" in front, it separates it from the digits.
Do you know how I can do this?
You can just extend twig with a custom filter which solves this for you, e.g.
$twig->addFilter(new \Twig\TwigFilter('format_phone', function($phone) {
$prefix = null;
if (strpos($phone, '+') === 0) $prefix = substr($phone, 0, 4).' ';
$temp = str_split(substr($phone, $prefix ? 4 : 0), 2);
return $prefix.implode(' ', $temp);
});
demo
{{ '0600000000'|format_phone }}
{{ '+336000000'|format_phone }}
Related
I have this test wallet:
% solana account 9AYXoP3b22KHRhVuvjZbynpjNd2SXKFLoC1jrPMkyFkS
Public Key: 9AYXoP3b22KHRhVuvjZbynpjNd2SXKFLoC1jrPMkyFkS
Balance: 12.98943736 SOL
Owner: (removed by me)
Executable: false
Rent Epoch: 248
And I have this javascript snippet:
const SOURCE_TOKEN_ACCOUNT = new PublicKey('9AYXoP3b22KHRhVuvjZbynpjNd2SXKFLoC1jrPMkyFkS');
const sourceInfo = await connection.getParsedAccountInfo(
SOURCE_TOKEN_ACCOUNT,
);
console.log('tok ', sourceInfo);
When I run, I get this output:
tok { context: { slot: 191644 }, value: null }
I would like to note that I'm trying to get this to function with multiple accounts. They all have the same slot value.
I want to know why value would be coming back null. I have nothing to go on with why this would be happening. I just want some value to show up. Since I don't know what is supposed to show up when this works, I don't know what value is should be, short of showing some sort of SOL. All I know is, I'm getting the following error:
TypeError: Cannot read properties of null (reading 'data')
Accounts in Solana store lamports (SOL) and data, and in this case, your account only has SOL in it, and no data, which is why the value is coming up as null. For example, the token program at TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA will also show the bytes of the data in the account along with the balance:
$ solana account TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA
Public Key: TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA
Balance: 1.08999168 SOL
Owner: BPFLoader2111111111111111111111111111111111
Executable: true
Rent Epoch: 161
Length: 156480 (0x26340) bytes
0000: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 .ELF............
.... rest omitted for length ....
If you want data inside your account, you must allocate data to it, assign it to a program, and use a transaction on the program to modify the data.
I would highly recommend reading up more on how accounts work: https://docs.solana.com/developing/programming-model/accounts
This is my first question here, so hope it's correctly done.
Im trying to get some information from a ZTE C300 OLT.
The thing is when i try to get the SN of one of the ONTS I get the response in HEX-String
snmpwalk -cpublic -v2c [OLTIP] 1.3.6.1.4.1.3902.1082.500.10.2.2.5.1.2
And this is the response that I get
SNMPv2-SMI::enterprises.3902.1082.500.10.2.2.5.1.2.285278736.1 = Hex-STRING: 5A 54 45 47 C8 79 9B 27
This is the SN that i have on the OLT ZTEGC8799B27, but im trying to convert the HEX-STRING into text and i don't get that SN text.
Indeed i have a python script for SNMP and the response that i get for that OID is
{'1.3.6.1.4.1.3902.1082.500.10.2.2.5.1.2.285278736.1': "ZTEGÈy\x9b'"}
Can someone give me a hand on this?. I'm new on SNMP and this is giving me some headache.
Thanks in advace!
This is a 8 octet hex string, the first 4 octets are ASCII.
Just convert hex 2 ascii.
Indeed it was easier. The firts 4 bytes were encoded, and the other 4 is the actual serial number splitted every 2 digits. So i only need to decode the first part and concatenate the rest.
Works with OLT ZTE C320
def hex_str(str):
str = str.strip()
str = str.split(' ')
vendor_id = ''
serial = str[4:]
serial = "".join(serial)
for hex_byte in str[:4]:
vendor_id += chr(int(hex_byte, 16))
normalized_serial = vendor_id + serial
return normalized_serial
def ascii_to_hex(str):
arr = []
hex_byte = ''
for i in range(len(str)):
hex_byte += hex(ord(str[i]))
hex_byte = hex_byte.replace('0x', ' ')
hex_byte = hex_str(hex_byte)
return hex_byte
# value = f"5A 54 45 47 C8 79 9B 27 "
# value = f"49 54 42 53 8B 69 A2 45 "
# value = f"ZTEGÈy\x9b'"
value = f"ITBS2Lz/"
# value = f"ITBS2HP#"
if(len(value) == 24):
print(hex_str(value))
else:
print(ascii_to_hex(value))
I need to generate a random 128-bit number and get it's binary representation, b64-encoded.
Sample:
vagrant#ubuntu1804:~$ consul keygen
TUlzi8BWwPQR1zyjR1TiAQ==
in it's b64 decoded form:
vagrant#ubuntu1804:~$ consul keygen | base64 -d|hexdump -C
00000000 46 b8 72 4b ce 9a 2a 14 09 7b 16 51 99 1b 39 e0 |F�rK�.*..{.Q..9�|
00000010
I can generate the large number with random filter:
{{ 340282366920938463463374607431768211456 | random }}
I know I can encode it with b64encode, but have no idea how to convert the number to the binary format.
Instead of converting (which is impossible as you can't store/process binary using native Ansible data structures), write a simple filter plugin (filter_plugins/myfilters.py) which would generate the content you want:
import os
import base64
class FilterModule(object):
def filters(self):
return {
'binary_random_b64_encoded': self.binary_random_b64_encoded
}
def binary_random_b64_encoded(self, size):
return base64.b64encode(os.urandom(size)).decode('ascii')
and use it (with the requested size of the binary data):
- debug:
msg: "{{ 16 | binary_random_b64_encoded }}"
I am trying to read data from sony felica card using pc/sc transparent session and transceive data object.
The response I am getting is for a read without encryption command is
c0 03 00 90 00 92 01 00 96 02 00 00 97 82 00 + Data
But according to the protocol, the response should be
c0 03 00 90 00 92 01 00 96 02 00 00 97 + Data
I am unable to figure out the last 82 00 appended in the response from the card.
Now when I try to authenticate with the card I get
c0 03 01 6F 01 90 00
which is a error in pc/sc. I want to resolve these extra bytes 82 00 which I believe will solve the issue with all the commands which require authentication and encryption.
The response data is BER-TLV encoded (see PC/SC 2.02, Part 3).
In BER-TLV encoding there are several possibilities to encode tag 0x97 with two octets of data 0xD0D1, e.g.:
97|02|D0D1 -- short form (see parsed)
97|8102|D0D1 -- long form with one octet with length (see parsed)
97|820002|D0D1 -- long form with two octets with length (see parsed)
97|83000002|D0D1 -- long form with three octets with length (see parsed)
...
Your reader is using two octets for sending the length of ICC Response data object (which is perfectly valid).
You should parse the response properly...Good luck!
PS: The above means, that the Data part of your truncated responses still contains one extra byte with the response length (i.e. Len|Data)
Say I have a function called drawGraphics which runs very tight for 3 seconds under some predefined configuration.
In that function I may call myDecorator.decorate(), given that the field is not null.
I can run this code using two options:
if (myDecorator != null)
myDecorator.decorate();
-or-
// during init:
isUsingDecorator = myDecorator != null; // boolean field
// ...
// during 'drawGraphics'
if (isUsingDecorator)
myDecorator.decorate();
Which is more efficient: comparing a field to null or asking if a boolean field is 'true' (or comparing an int field to 0) ?
Am I being over dramatic about performance here??
Thanks in advance
Eyal
Most likely resolving names takes far longer than actual comparison. But this depends greatly on your code structure, on the nesting of scopes and on the optimisations the compiler uses with it. I doubt that this very issue really matters in overall performance. I would guess that you are micro-optimizing here.
Anyway, what you could do to assess the issue is take a look at the bytecode that gets generated. Strip your code of irrelevant parts preserving the structure of scopes and decompile the result.
I.e. suppose you have this code in a single frame of an empty bytecode.swf movie:
var test:Function = function(){};
var check:Boolean = test != null;
var action:Function = function()
{
if (test != null) {
trace(1);
}
if (check) {
trace(2);
}
};
action();
Use flex_sdk_4.6\bin\swfdump.exe:
swfdump.exe -abc -showbytecode bytecode.swf > bytecode.txt
Inspect bytecode.txt and find the following:
02 02 01 0B 0B 1C var null::no name():
maxStack:2 localCount:1 initScopeDepth:11 maxScopeDepth:11
60 03 getlex :test
20 pushnull
13 07 00 00 ifeq L0
5D 09 findpropstrict :trace
24 01 pushbyte 1
4F 09 01 callpropvoid :trace (1)
60 05 L0: getlex :check
12 07 00 00 iffalse L1
5D 09 findpropstrict :trace
24 02 pushbyte 2
4F 09 01 callpropvoid :trace (1)
47 L1: returnvoid
Now we can see that comparing test to null takes three instructions: getlex, pushnull, ifeq; and checking a boolean results in two instructions: getlex, iffalse. And the only thing that matters here performance-wise is resolving identifiers with getlex.
So, to answer your question you need to figure out how long does it really take to resolve myDecorator in your particular context. For example, if isUsingDecorator is a local to method variable and myDecorator is not, you will definitely get better performance with the former. Once again, I doubt that this is what really matters.
P.S. You may as well use a primitive test below, but it's highly inaccurate when the difference in performance is so tiny or non-existent. Anyway, it can give you a hint at least: this is not what needs optimisation.
import flash.utils.setInterval;
var test:Function = function(){};
var check:Boolean = test != null;
var action:Function = function()
{
var a:Date;
var s:int, i:int;
s = getTimer();
for(i = 0;i<100000;i++) {
if (test != null) {
a = new Date(); // just chewing the fat
}
}
trace("a:"+(getTimer()-s));
s = getTimer();
for(i = 0;i<100000;i++) {
if (check) {
a = new Date(); // just chewing the fat
}
}
trace("b:"+(getTimer()-s));
};
setInterval(action, 1000);
This is hardly a relevant question since your second case is the first case + assignment. Second case for that reason simply cannot be faster.
Why also limiting to 2 options? Evaluating the instance directly is also perfectly valid:
if(myDecorator)
{ etc ....