Flutter - Dart - remove hidden character from String input - list

I'm trying to compare 2 strings, however my comparison always fails.
For reference, the one string is a filename I'm getting from the phones storage and it look like it ends with an apostrophe, although its not visible anywhere.
Please consider the following dart code:
import 'dart:convert';
void main() {
const Utf8Codec utf8 = Utf8Codec();
String input = 'chatnum.txt';
String stringwithapostrophe = 'chatnum.txt\'';
String compInput = utf8.encode(input).toString();
String compComp = utf8.encode(stringwithapostrophe).toString();
print (compInput);
print (compComp);
if (compInput == compComp) {
print ('Yes it matches');
} else {
print ('No it does not');
}
}
This output's a result of:
[99, 104, 97, 116, 110, 117, 109, 46, 116, 120, 116]
[99, 104, 97, 116, 110, 117, 109, 46, 116, 120, 116, 39]
No it does not
So how can I remove that last apostrophe from the String?
I've tried .removeAt and .removeLast. But I just can't crack this.

I applied regex to it. That sorted it:
String filenametosend = (basename(f.toString()))
.replaceAll(RegExp(r"[-!$%^&*()+|~=`{}##\[\]:;'’<>?,\/"
'"”'
"]"), '');

This way too:
final apostrophe = '\'';
final length = stringwithapostrophe.length;
if (length > 0 && stringwithapostrophe[length - 1] == apostrophe) {
stringwithapostrophe = stringwithapostrophe.substring(0, length - 1);
}
Or this way (remove all):
final apostrophe = '\'';
stringwithapostrophe = stringwithapostrophe.replaceAll(apostrophe, '');
Remove (any) last:
final length = stringwithapostrophe.length;
stringwithapostrophe = length > 0
? stringwithapostrophe.substring(0, length - 1)
: stringwithapostrophe;

Related

Python sys.meta_path and strange module requests

Importing is behaving inconsistently and I'm pretty sure it's because of my meta_path loader.
My importer
class JaPyCodeImporter(object):
def __init__(self, modules):
self._modules = {}
self._last_module = None
modules = dict(modules)
for key in modules:
module_info = {}
module_info["full_name"] = key
module_info["source"] = modules[key]
module_info["code"] = compile(modules[key], key, "exec")
module_info["is_package"] = False
module_info["package"] = key.rpartition('.')[0]
if not key.rfind("__init__") == -1:
module_info["full_name"] = key[:key.rfind(".__init__")]
module_info["is_package"] = True
module_info["package"] = module_info["full_name"]
self._modules[module_info["full_name"]] = module_info
self._last_path = ""
def find_module(self, fullname, path):
if fullname in self._modules.keys():
print("Found: {0}".format(fullname))
return self
elif self._last_module and self._last_module["package"] + "." + fullname in self._modules.keys():
print("Found: {0} in package {1}".format(fullname, self._last_module["package"]))
return self
print("Couldn't find: {0}".format(fullname))
pdb.traceback.print_stack()
return None
def load_module(self, fullname):
expanded_name = self._last_path + "." + fullname
# check for pre-existence
if fullname in sys.modules:
return sys.modules[fullname]
elif self._last_module and self._last_module["package"] + "." + fullname in sys.modules:
return sys.modules[self._last_module["package"] + "." + fullname]
# check internally
if not fullname in self._modules.keys():
if self._last_module and not self._last_module["package"] + "." + fullname in self._modules.keys():
raise ImportError(fullname)
else:
fullname = self._last_module["package"] + "." + fullname
module_info = self._modules[fullname]
new_module = sys.modules.setdefault(fullname, imp.new_module(fullname))
new_module.__loader__ = self
new_module.__file__ = "<%s>" % self.__class__.__name__
new_module.__path__ = [] #module_info["package"].split(".")
new_module.__package__ = module_info["package"]
print("Loaded: {0}".format(module_info["full_name"]))
try:
self._last_module = self._modules[fullname]
print("Execute: {0}".format(module_info["full_name"]))
globals()["__name__"] = module_info["full_name"]
globals()["__package__"] = module_info["package"]
exec(self._modules[fullname]["code"], globals(), new_module.__dict__)
except SyntaxError as err:
error_class = err.__class__.__name__
detail = err.args[0]
line_number = err.lineno
print("%s at line %d of %s: %s" % (error_class, line_number, description, detail))
del sys.modules[fullname]
return None
except Exception as err:
error_class = err.__class__.__name__
detail = err.args[0]
print(detail)
cl, exc, tb = sys.exc_info()
line_number = traceback.extract_tb(tb)[-1][1]
print("%s at line %d of %s: %s" % (error_class, line_number, fullname, detail))
del sys.modules[fullname]
return None
return new_module
My file structure
gameSystem/
__init.__py
interface_bridge.py
libraries/
__init__.py
standard/
__init__.py
area.py
base_object.py
exit.py
player.py
Start of interface_bridge.py
print("*** begin interface_bridge.py ***")
# from https://github.com/jython/jython/blob/0a58cc26566d2b2334e80b2b3f2f42f6c738db2d/lib-python/2.7/ihooks.py
# line 424
print("__name__: {0}".format(globals()['__name__']))
# line 418
print("__package__: {0}".format(globals()['__package__']))
print("Begin imports")
import pickle
import io
import sys
print(dir(sys.modules[__name__]))
from . libraries.standard.player import Player
print(Player)
from . libraries.standard.player import Player # <- Line 22
#from . libraries.standard.area import Area
Yep, I'm literally trying to import Player a second time directly after the first. I'm doing that because I can see from the output that Player is imported correctly the first time. Regardless, if I use the second Player import or the commented Area import, the output is the same.
The output
*** begin interface_bridge.py ***
__name__: gameSystem.interface_bridge
__package__: gameSystem
Begin imports
Couldn't find: gameSystem.pickle
File "<iostream>", line 236, in loadGameData
File "<iostream>", line 116, in load_module
File "gameSystem.interface_bridge", line 13, in <module>
File "<iostream>", line 66, in find_module
Couldn't find: gameSystem.io
File "<iostream>", line 236, in loadGameData
File "<iostream>", line 116, in load_module
File "gameSystem.interface_bridge", line 14, in <module>
File "<iostream>", line 66, in find_module
Couldn't find: gameSystem.sys
File "<iostream>", line 236, in loadGameData
File "<iostream>", line 116, in load_module
File "gameSystem.interface_bridge", line 15, in <module>
File "<iostream>", line 66, in find_module
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', 'io', 'pickle', 'sys']
Found: gameSystem.libraries
Loaded: gameSystem.libraries
Execute: gameSystem.libraries
Found: gameSystem.libraries.standard
Loaded: gameSystem.libraries.standard
Execute: gameSystem.libraries.standard
Found: gameSystem.libraries.standard.player
Loaded: gameSystem.libraries.standard.player
Execute: gameSystem.libraries.standard.player
<class 'gameSystem.libraries.standard.player.Player'> <- Player class loaded!
Couldn't find: gameSystem.libraries.standard.libraries
File "<iostream>", line 236, in loadGameData
File "<iostream>", line 116, in load_module
File "gameSystem.interface_bridge", line 22, in <module>
File "<iostream>", line 66, in find_module
Couldn't find: gameSystem.libraries.standard.libraries
File "<iostream>", line 236, in loadGameData
File "<iostream>", line 116, in load_module
File "gameSystem.interface_bridge", line 22, in <module>
File "<iostream>", line 66, in find_module
No module named gameSystem.libraries.standard.libraries
ImportError at line 22 of gameSystem.interface_bridge: No module named gameSystem.libraries.standard.libraries
Those first three couldn't finds (pickle, io, and sys) all make sense as they are Python libraries (though it's interesting to see them being treated as if their part of the gameSystem package). My importer shouldn't handle them and kicks them back to the import system.
Then, as I interpret it, the import system starts working down the package structure (.libraries, .libraries.standard) to finally get to gameSystem.libraries.standard.player, which is loaded and executed without problem. You can see the class is ready to go as it can be printed to the output <class '...Player'>.
What happens next is where I get confused. Why is it trying to import gameSystem.libraries.standard.libraries? Why two times in a row?
I'm fairly certain this is caused by some mistake I've made in my importer but I'm baffled. I would expect both import lines to behave exactly the same way.

Python 3 replace part of string with data from dict

I need to turn phonenumbers into international format. I have a list of phone number examples
rows = [
(datetime.time(20, 35, 30), '0707262078',),
(datetime.time(20, 38, 18), '+46706332602',),
(datetime.time(20, 56, 35), '065017063'),
(datetime.time(21, 45, 1), '+46730522807',),
(datetime.time(22, 13, 47), '0046733165812')
]
I need to replace all numbers starting with ex 07 with +467, all 06 with +466 and 00 with +. For above example I need the number to turn out 0707262078 to +46707262078, 065017063 to +4665017063 and 0046733165812 to +46733165812.
Dont know if its possible to do this in regex only or if I need to do it with other code.
Been trying with re.sub combined with lamda, my thought is to make a dictionary with the matching replaces like this:
repl_dict = {
'01': '+461',
'02': '+462',
'03': '+463',
'04': '+464',
'05': '+465',
'06': '+466',
'07': '+467',
'08': '+468',
'09': '+469',
'00': '+'
}
My try so far:
import re
rows = [
(datetime.time(20, 35, 30), '0707262078',),
(datetime.time(20, 38, 18), '+46706332602Ring via Mitel ',),
(datetime.time(20, 56, 35), '065017063'),
(datetime.time(21, 45, 1), '+46730522807Ring via Mitel ',),
(datetime.time(22, 13, 47), '0046733165812')
]
repl_dict = {
'01': '+461',
'02': '+462',
'03': '+463',
'04': '+464',
'05': '+465',
'06': '+466',
'07': '+467',
'08': '+468',
'09': '+469',
'00': '+'
}
for row in rows:
regex = re.compile(r'^\d{1}[0-9](\d*)'), re.S
DialedNumber = regex.sub(lambda match: repl_dict.get(match.group(0), row[1]), row[1], row[1])
Your regex, ending in \d*, will match the entire number, and hence no entry is found in the dict. Also, there seems to be an unmatched parens and one too many row[1] in the call to sub.
You can simplify your regex to ^00? and your replacements dict to {'00': '+', '0': '+46'}. This will check whether the number starts with either one or two 0, making the replacement dict much simpler and less repetetive.
rows = [(datetime.time(20, 35, 30), '0707262078',), (datetime.time(20, 38, 18), '+46706332602Ring via Mitel ',), (datetime.time(20, 56, 35), '065017063'), (datetime.time(21, 45, 1), '+46730522807Ring via Mitel ',), (datetime.time(22, 13, 47), '0046733165812')]
repl_dict = {'00': '+', '0': '+46'}
regex = re.compile(r'^00?')
for date, number in rows:
print(regex.sub(lambda match: repl_dict.get(match.group(0)), number))
Output:
+46707262078
+46706332602Ring via Mitel
+4665017063
+46730522807Ring via Mitel
+46733165812
If you only want the numeric part, you can pre- or postprocess the numbers with a second regex like [0-9+]*.
This is the naive approach based on repl_dict as given in your question.
def repl(match):
return repl_dict[match.group(0)]
pat = '^(' + '|'.join(repl_dict) + ')'
new_rows = [(tm, re.sub(pat, repl, ph)) for (tm, ph) in rows]
tobias_k's answer gives a better approach by improving your repl_dict and pattern.
Regex: ^[0-9]{2}
Details:
^ Asserts position at start of a line
[] Match a single character present in the list
{n} Matches exactly n times
Python code:
By #tobias_k you can use repl_dict.get(m.group(), m.group()) instead of repl_dict.get(m.group()) or m.group().
regex = re.compile(r'^[0-9]{2}')
for i in range(len(rows)):
rows[i] = (rows[i][0], regex.sub(lambda m: repl_dict.get(m.group()) or m.group(), rows[i][1]))
Output:
[(datetime.time(20, 35, 30), '+46707262078'), (datetime.time(20, 38, 18), '+46706332602Ring via Mitel '), (datetime.time(20, 56, 35), '+4665017063'), (datetime.time(21, 45, 1), '+46730522807Ring via Mitel '), (datetime.time(22, 13, 47), '+46733165812')]
Code demo
You can do it without a regex:
for row in rows:
for repl in repl_dict:
if row[1].startswith(repl):
print repl_dict[repl]+row[1][len(repl):]
break

How to handle "cannot refer to an open group at position" in Python?

With modified code taken from Python docs, in the tokenizer below I have added regular expression to match docstrings, comments and quotes which are combined into a single master regular expression and successive matches are looped over.
import collections
import re
Token = collections.namedtuple('Token', ['typ', 'value', 'line', 'column'])
def tokenize(code):
keywords = {'IF', 'THEN', 'ENDIF', 'FOR', 'NEXT', 'GOSUB', 'RETURN'}
token_specification = [
('DOC', r'([\'"]{3})[^\x00]*?\2'), # docstrings
('COMM', r'#.*'), # comments
('QUOT', r'(?:"([^"\\\\]*(?:\\.[^"\\\\]*)*)"|' # quotes
r'\'([^\'\\]*(?:\\.[^\'\\]*)*)\')|'
r'r\'([^"(]*)\((.*?)\)\4\'')
]
tok_regex = '|'.join('(?P<%s>%s)' % pair for pair in token_specification)
line_num = 1
line_start = 0
for mo in re.finditer(tok_regex, code):
kind = mo.lastgroup
value = mo.group(kind)
if kind == 'NEWLINE':
line_start = mo.end()
line_num += 1
elif kind == 'SKIP':
pass
elif kind == 'MISMATCH':
raise RuntimeError('%r unexpected on line %d' % (value, line_num))
else:
if kind == 'ID' and value in keywords:
kind = value
column = mo.start() - line_start
yield Token(kind, value, line_num, column)
statements = '''
"""docstr1
blah"""
\'''docstr2\'''
# ok
IF "okkk" and 'ole' quantity THEN
total := total + price * quantity;
tax := price * 0.05;
ENDIF;
'''
for token in tokenize(statements):
print(token)
The above gives the following error:
line 72, in <module>
for token in tokenize(statements),
line 44, in tokenize
for mo in re.finditer(tok_regex, code),
line 220, in finditer
return _compile(pattern, flags).finditer(string),
line 293, in _compile
p = sre_compile.compile(pattern, flags),
line 536, in compile
p = sre_parse.parse(p, flags),
line 829, in parse
p = _parse_sub(source, pattern, 0),
line 437, in _parse_sub
itemsappend(_parse(source, state)),
line 778, in _parse
p = _parse_sub(source, state),
line 437, in _parse_sub
itemsappend(_parse(source, state)),
line 524, in _parse
code = _escape(source, this, state),
line 415, in _escape
len(escape)) sre_constants.error: cannot refer to an open group at position 142
I understand that the problem is the fact that the QUOT regex should be matched before the other two regexes (in case this is too simplistic or plain wrong please do explain).
I am afraid that on some occasions I may end up messing the order of the regexes and not being able to get an error in order to fix it until I provide an appropriate statements.
My question is how could I be able to handle such cases/errors gracefully? Is there a try .. except approach appropriate? Any code example to illustrate this would be great.
(using Python 3.5.1)

Negating a regex

How do I negate the regex [a-zA-Z]+[0-9]*? i.e. from a string '12-mar-14, 21, 123_4, Value123, USER, 12/2/13' I need to match values anything other than Value123 and USER. Can someone please explain how?
I'm trying to replace the string '12-mar-14, 21, 123_4, Value123, USER, 12/2/13' to '%Value123%USER%' in Java. Anything that doesn't match [a-zA-Z]+[0-9]* should be replaced with %
A regex that would give following outputs for corresponding inputs.
Input: '12-mar-14, 21, 123_4, Value123, USER, 12/2/13'
Output: '%Value123%USER%'
Input: '12-mar-14, 21, 123_4'
Output: '%'
Input: 'New, 12-Mar-14, 123, dat_123, Data123'
Output: '%New%Data123%'
Use this method:
//********** MODIFIED *************//
public static void getSentence(String line) {
String text[] = line.split(",");
String res = "";
for (int i = 0; i < text.length; i++) {
String word = text[i].trim();
if (word.matches("[a-zA-Z]+[0-9]*")){
if (!"".equals(res))
res = res + "%";
res = res + word;
}
}
if ("".equals(res))
res = "%";
else
res = "%" + res + "%";
System.out.println(res);
}
...
this.getSentence("New, 12-Mar-14, 123, dat_123, Data123");
this.getSentence("12-mar-14, 21, 123_4, Value123, USER, 12/2/13");
Output:
%New%Data123%
%Value123%USER%

Lua Control Statments having odd behavior

I'm getting a very unexpected result from what should be basic control statement operations. I have the following, a file being read with this sort of data:
1, 51, one , ab
1, 74, two , ab
0, 74, tree , ab
0, 74, for , ab
0, 74, five , ab
My snip of Lua code that processes it:
if file then
for line in file:lines() do
LineArray = line
CanClaimInfo[LineArray] = {}
lineData = utils.split(line,",")
if lineData[1] == "0" then
lineData[1] = "CAN A"
elseif lineData[1] == "1" then
lineData[1] = "CAN B"
else
lineData[1] = lineData[1]
end
CanClaimInfo[LineArray]["CANBus"] = lineData[1]
CanClaimInfo[LineArray]["Address"] = lineData[2]
CanClaimInfo[LineArray]["Name"] = lineData[3]
end
and I get this as an output:
CAN A 74 for
CAN A 74 tree
CAN A 74 five
CAN B 74 two
1 51 one
I don't get how it slips through the elseif lineData[1] == "1" then bit. I checked and there are no lead/trailing white spaces or anything like that. Any ideas?
Maybe utf-8 encoding bytes at the beginning of file? Try printing lineData[1] before the "if" tests to see what it is, and print(#lineData[1]) to see how many chars it has. Likely more than 1 char so it ends up in that third branch (else):
lineData = utils.split(line,",")
print(#lineData[1]) -- likely prints 1 for all but first line
if lineData[1] == "0" then
To find the extra bytes, try print(string.byte(lineData[1], 1, #lineData[1])).
Hm, seems like your utils.split function has some problems. I used a function from http://lua-users.org/wiki/SplitJoin and it works quite well with your code:
utils = {
split = function(str, pat)
local t = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
}
Maybe your function converts the 1 to a number (for whatever reason). In Lua, "1" ~= 1!