Best Python code snippet using slash
test_convert.py
Source:test_convert.py
1"""Test Convert() methods"""2import pytest3import time4import re5from simpleth import Blockchain, Convert, SimplEthError6# Ether denomination names to their value in wei7d2w = {8 'wei': 1,9 'kwei': 10**3,10 'babbage': 10**3,11 'femtoether': 10**3,12 'mwei': 10**6,13 'lovelace': 10**6,14 'picoether': 10**6,15 'gwei': 10**9,16 'shannon': 10**9,17 'nanoether': 10**9,18 'nano': 10**9,19 'szabo': 10**12,20 'microether': 10**12,21 'micro': 10**12,22 'finney': 10**15,23 'milliether': 10**15,24 'milli': 10**15,25 'ether': 10**18,26 'kether': 10**21,27 'grand': 10**21,28 'mether': 10**24,29 'gether': 10**27,30 'tether': 10**3031 }32# Test this first. Insure it is accurate and then test convert_ether().33def test_denominations_to_wei():34 """Return dictionary of denomination names and value in wei"""35 assert Convert().denominations_to_wei() == d2w36# This is a brute force test. It generates 23x23 test cases. These37# take about 50 seconds on my laptop to run.38@pytest.mark.skip(reason='takes too long')39@pytest.mark.parametrize('from_denominations', [40 'wei',41 'kwei',42 'babbage',43 'femtoether',44 'mwei',45 'lovelace',46 'picoether',47 'gwei',48 'shannon',49 'nanoether',50 'nano',51 'szabo',52 'microether',53 'micro',54 'finney',55 'milliether',56 'milli',57 'ether',58 'kether',59 'grand',60 'mether',61 'gether',62 'tether'63 ]64 )65@pytest.mark.parametrize('to_denominations', [66 'wei',67 'kwei',68 'babbage',69 'femtoether',70 'mwei',71 'lovelace',72 'picoether',73 'gwei',74 'shannon',75 'nanoether',76 'nano',77 'szabo',78 'microether',79 'micro',80 'finney',81 'milliether',82 'milli',83 'ether',84 'kether',85 'grand',86 'mether',87 'gether',88 'tether'89 ]90 )91def test_convert_ether_all(from_denominations, to_denominations):92 """convert_ether() returns accurate conversion for all combinations of93 to and from denominations"""94 amount = 1095 # Validate with web3.py conversion methods96 converted_amount = Blockchain().web3.fromWei(97 Blockchain().web3.toWei(amount, from_denominations),98 to_denominations99 )100 assert Convert().convert_ether(101 amount,102 from_denominations,103 to_denominations104 ) == converted_amount105@pytest.mark.parametrize('bad_denominations', ['xxx', 1234])106def test_convert_ether_bad_from(bad_denominations):107 """convert_ether() with bad from denomination raises SimplEthError"""108 with pytest.raises(SimplEthError) as excp:109 Convert().convert_ether(10, bad_denominations, 'ether')110 assert excp.value.code == 'V-010-010'111@pytest.mark.parametrize('bad_denominations', ['xxx', 1234])112def test_convert_ether_bad_to(bad_denominations):113 """convert_ether() with bad to denomination raises SimplEthError"""114 with pytest.raises(SimplEthError) as excp:115 Convert().convert_ether(10, 'ether', bad_denominations)116 assert excp.value.code == 'V-010-020'117def test_epoch_time():118 """epoch_time() returns a float and value increases over time"""119 t1 = Convert().epoch_time()120 time.sleep(.01)121 t2 = Convert().epoch_time()122 assert isinstance(t1, float) and (t2 > t1)123def test_local_time_string():124 """local_time_string() returns a formatted time string"""125 # default time string format: YYYY-MM-DD HH:MM:SS126 default_time_format = \127 "[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}"128 assert(129 re.match(130 default_time_format,131 Convert().local_time_string()132 )133 )134def test_local_time_with_time_format():135 """local_time() returns a specified formatted time string"""136 strftime = '%I:%M' # use a simple time string format of HH:MM137 time_format = "[0-9]{2}:[0-9]{2}"138 assert(139 re.match(140 time_format,141 Convert().local_time_string(strftime)142 )143 )144def test_local_time_string_raises_v_020_010():145 """local_time_string() with bad t_format type raises SimplEthError"""146 bad_format_type = 100147 with pytest.raises(SimplEthError) as excp:148 Convert().local_time_string(bad_format_type)149 assert excp.value.code == 'V-020-010'150def test_to_local_time_string_int():151 """to_local_time_string() with integer epoch seconds returns a152 formatted time string"""153 time_format = '%Y-%m-%d %H:%M:%S' # must match simpleth TIME_FORMAT154 test_epoch_sec = 1639932637155 test_local_time = time.strftime(156 time_format,157 time.localtime(test_epoch_sec)158 )159 assert(160 Convert().to_local_time_string(test_epoch_sec) == test_local_time161 )162def test_to_local_time_string_float():163 """to_local_time_string() returns a float epoch seconds returns a164 formatted time string"""165 time_format = '%Y-%m-%d %H:%M:%S' # must match simpleth TIME_FORMAT166 test_epoch_sec = 1639932637.00167 test_local_time = time.strftime(168 time_format,169 time.localtime(test_epoch_sec)170 )171 assert(172 Convert().to_local_time_string(test_epoch_sec) == test_local_time173 )174def test_to_local_time_with_time_format():175 """to_local_time() returns a specified formatted time string"""176 t_format = '%I:%M' # use a simple time string format of HH:MM177 test_epoch_sec = 1639932637178 test_local_time = time.strftime(179 t_format,180 time.localtime(test_epoch_sec)181 )182 assert(183 Convert().to_local_time_string(test_epoch_sec, t_format) ==184 test_local_time185 )186def test_to_local_time_string_raises_v_030_010():187 """to_local_time_string() with bad t_format type raises SimplEthError"""188 bad_format_type = 100189 test_epoch_sec = 1639932637190 with pytest.raises(SimplEthError) as excp:191 Convert().to_local_time_string(test_epoch_sec, bad_format_type)...
log_time_tamer.py
Source:log_time_tamer.py
1#!/usr/bin/env python2'''3Parses a guilog current or last whose file is specified in 'filepath'4and changes all UTC reocrded timestamps to local time specified in the5timezone().6Use ./log_time_tamer.py > test_local_time.txt (pipe to file)7'''8from datetime import datetime, date9import pytz10from pytz import timezone11filepath = '/home/atp/Downloads/customer_debugging/stratis/17.07.10/current'12def get_my_timezone_time(utc_time_str):13 if len(utc_time_str.strip()) is not 15:14 return '*'+utc_time_str+'*'15 try:16 woutc = datetime.strptime(utc_time_str, "%Y%m%d-%H%M%S") #without UTC [time naive]17 except:18 return '*'+utc_time_str+'*'19 wutc = pytz.utc.localize(woutc) #localized with UTC [time aware]20 #print wutc21 myztime = wutc.astimezone(timezone('Asia/Singapore')) #convert to Zone22 return myztime.strftime("%Y%m%d-%H%M%S")23mfile = open(filepath, 'r')24mfind = '201707'25for mline in mfile:26 if mfind in mline:27 mline.strip()28 start = mline.find(mfind)29 utc_time_str = mline[start:start+15]30 my_time_str = get_my_timezone_time(utc_time_str)31 mline = mline.replace(utc_time_str, my_time_str)32 print mline.strip()33 else:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!