idx int64 0 63k | question stringlengths 61 4.03k | target stringlengths 6 1.23k |
|---|---|---|
24,200 | def mapToPixel ( mX , mY , geoTransform ) : mX = np . asarray ( mX ) mY = np . asarray ( mY ) if geoTransform [ 2 ] + geoTransform [ 4 ] == 0 : pX = ( ( mX - geoTransform [ 0 ] ) / geoTransform [ 1 ] ) - 0.5 pY = ( ( mY - geoTransform [ 3 ] ) / geoTransform [ 5 ] ) - 0.5 else : pX , pY = applyGeoTransform ( mX , mY , i... | Convert map coordinates to pixel coordinates based on geotransform Accepts float or NumPy arrays |
24,201 | def pixelToMap ( pX , pY , geoTransform ) : pX = np . asarray ( pX , dtype = float ) pY = np . asarray ( pY , dtype = float ) pX += 0.5 pY += 0.5 mX , mY = applyGeoTransform ( pX , pY , geoTransform ) return mX , mY | Convert pixel coordinates to map coordinates based on geotransform Accepts float or NumPy arrays |
24,202 | def mem_ds ( res , extent , srs = None , dtype = gdal . GDT_Float32 ) : dst_ns = int ( ( extent [ 2 ] - extent [ 0 ] ) / res + 0.99 ) dst_nl = int ( ( extent [ 3 ] - extent [ 1 ] ) / res + 0.99 ) m_ds = gdal . GetDriverByName ( 'MEM' ) . Create ( '' , dst_ns , dst_nl , 1 , dtype ) m_gt = [ extent [ 0 ] , res , 0 , exte... | Create a new GDAL Dataset in memory |
24,203 | def copyproj ( src_fn , dst_fn , gt = True ) : src_ds = gdal . Open ( src_fn , gdal . GA_ReadOnly ) dst_ds = gdal . Open ( dst_fn , gdal . GA_Update ) dst_ds . SetProjection ( src_ds . GetProjection ( ) ) if gt : src_gt = np . array ( src_ds . GetGeoTransform ( ) ) src_dim = np . array ( [ src_ds . RasterXSize , src_ds... | Copy projection and geotransform from one raster file to another |
24,204 | def geom_transform ( geom , t_srs ) : s_srs = geom . GetSpatialReference ( ) if not s_srs . IsSame ( t_srs ) : ct = osr . CoordinateTransformation ( s_srs , t_srs ) geom . Transform ( ct ) geom . AssignSpatialReference ( t_srs ) | Transform a geometry in place |
24,205 | def shp_dict ( shp_fn , fields = None , geom = True ) : from pygeotools . lib import timelib ds = ogr . Open ( shp_fn ) lyr = ds . GetLayer ( ) nfeat = lyr . GetFeatureCount ( ) print ( '%i input features\n' % nfeat ) if fields is None : fields = shp_fieldnames ( lyr ) d_list = [ ] for n , feat in enumerate ( lyr ) : d... | Get a dictionary for all features in a shapefile Optionally specify fields |
24,206 | def lyr_proj ( lyr , t_srs , preserve_fields = True ) : s_srs = lyr . GetSpatialRef ( ) cT = osr . CoordinateTransformation ( s_srs , t_srs ) drv = ogr . GetDriverByName ( 'Memory' ) out_ds = drv . CreateDataSource ( 'out' ) outlyr = out_ds . CreateLayer ( 'out' , srs = t_srs , geom_type = lyr . GetGeomType ( ) ) if pr... | Reproject an OGR layer |
24,207 | def raster_shpclip ( r_fn , shp_fn , extent = 'raster' , bbox = False , pad = None , invert = False , verbose = False ) : from pygeotools . lib import iolib from pygeotools . lib import warplib r_ds = iolib . fn_getds ( r_fn ) r_srs = get_ds_srs ( r_ds ) r_extent = ds_extent ( r_ds ) r_extent_geom = bbox2geom ( r_exten... | Clip an input raster by input polygon shapefile for given extent |
24,208 | def geom2shp ( geom , out_fn , fields = False ) : from pygeotools . lib import timelib driverName = "ESRI Shapefile" drv = ogr . GetDriverByName ( driverName ) if os . path . exists ( out_fn ) : drv . DeleteDataSource ( out_fn ) out_ds = drv . CreateDataSource ( out_fn ) out_lyrname = os . path . splitext ( os . path .... | Write out a new shapefile for input geometry |
24,209 | def get_outline ( ds , t_srs = None , scale = 1.0 , simplify = False , convex = False ) : gt = np . array ( ds . GetGeoTransform ( ) ) from pygeotools . lib import iolib a = iolib . ds_getma_sub ( ds , scale = scale ) geom = ogr . Geometry ( ogr . wkbPolygon ) if a . count ( ) != 0 : if ( scale != 1.0 ) : gt [ 1 ] *= s... | Generate outline of unmasked values in input raster |
24,210 | def ds_cT ( ds , x , y , xy_srs = wgs_srs ) : ds_srs = get_ds_srs ( ds ) mX = x mY = y if xy_srs is not None : if not ds_srs . IsSame ( xy_srs ) : mX , mY , mZ = cT_helper ( x , y , 0 , xy_srs , ds_srs ) return mX , mY | Convert input point coordinates to map coordinates that match input dataset |
24,211 | def line2pts ( geom , dl = None ) : nodes = geom . GetPoints ( ) if dl is None : nsteps = 1000 dl = geom . Length ( ) / nsteps l = [ ] mX = [ ] mY = [ ] l += [ 0 ] x = nodes [ 0 ] [ 0 ] y = nodes [ 0 ] [ 1 ] mX += [ x ] mY += [ y ] rem_l = 0 last_l = l [ - 1 ] for i in range ( 0 , len ( nodes ) - 1 ) : x1 , y1 = nodes ... | Given an input line geom generate points at fixed interval Useful for extracting profile data from raster |
24,212 | def get_res_stats ( ds_list , t_srs = None ) : if t_srs is None : t_srs = get_ds_srs ( ds_list [ 0 ] ) res = np . array ( [ get_res ( ds , t_srs = t_srs ) for ds in ds_list ] ) min = np . min ( res ) max = np . max ( res ) mean = np . mean ( res ) med = np . median ( res ) return ( min , max , mean , med ) | Return resolution stats for an input dataset list |
24,213 | def get_res ( ds , t_srs = None , square = False ) : gt = ds . GetGeoTransform ( ) ds_srs = get_ds_srs ( ds ) res = [ gt [ 1 ] , np . abs ( gt [ 5 ] ) ] if square : res = [ np . mean ( res ) , np . mean ( res ) ] if t_srs is not None and not ds_srs . IsSame ( t_srs ) : if True : extent = ds_extent ( ds , t_srs ) diag =... | Get GDAL Dataset raster resolution |
24,214 | def get_center ( ds , t_srs = None ) : gt = ds . GetGeoTransform ( ) ds_srs = get_ds_srs ( ds ) center = [ gt [ 0 ] + ( gt [ 1 ] * ds . RasterXSize / 2.0 ) , gt [ 3 ] + ( gt [ 5 ] * ds . RasterYSize / 2.0 ) ] if t_srs is not None and not ds_srs . IsSame ( t_srs ) : ct = osr . CoordinateTransformation ( ds_srs , t_srs )... | Get center coordinates of GDAL Dataset |
24,215 | def get_ds_srs ( ds ) : ds_srs = osr . SpatialReference ( ) ds_srs . ImportFromWkt ( ds . GetProjectionRef ( ) ) return ds_srs | Get srs object for GDAL Datset |
24,216 | def srs_check ( ds ) : gt = np . array ( ds . GetGeoTransform ( ) ) gt_check = ~ np . all ( gt == np . array ( ( 0.0 , 1.0 , 0.0 , 0.0 , 0.0 , 1.0 ) ) ) proj_check = ( ds . GetProjection ( ) != '' ) out = False if gt_check and proj_check : out = True return out | Check validitiy of Dataset srs |
24,217 | def ds_IsEmpty ( ds ) : out = False b = ds . GetRasterBand ( 1 ) try : mm = b . ComputeRasterMinMax ( ) if ( mm [ 0 ] == mm [ 1 ] ) : ndv = b . GetNoDataValue ( ) if ndv is None : out = True else : if ( mm [ 0 ] == ndv ) : out = True except Exception : out = True return out | Check to see if dataset is empty after warp |
24,218 | def gt_corners ( gt , nx , ny ) : ul = [ gt [ 0 ] , gt [ 3 ] ] ll = [ gt [ 0 ] , gt [ 3 ] + ( gt [ 5 ] * ny ) ] ur = [ gt [ 0 ] + ( gt [ 1 ] * nx ) , gt [ 3 ] ] lr = [ gt [ 0 ] + ( gt [ 1 ] * nx ) , gt [ 3 ] + ( gt [ 5 ] * ny ) ] return ul , ll , ur , lr | Get corner coordinates based on input geotransform and raster dimensions |
24,219 | def ds_geom ( ds , t_srs = None ) : gt = ds . GetGeoTransform ( ) ds_srs = get_ds_srs ( ds ) if t_srs is None : t_srs = ds_srs ns = ds . RasterXSize nl = ds . RasterYSize x = np . array ( [ 0 , ns , ns , 0 , 0 ] , dtype = float ) y = np . array ( [ 0 , 0 , nl , nl , 0 ] , dtype = float ) x -= 0.5 y -= 0.5 mx , my = pix... | Return dataset bbox envelope as geom |
24,220 | def geom_wh ( geom ) : e = geom . GetEnvelope ( ) h = e [ 1 ] - e [ 0 ] w = e [ 3 ] - e [ 2 ] return w , h | Compute width and height of geometry in projected units |
24,221 | def gdaldem_mem_ma ( ma , ds = None , res = None , extent = None , srs = None , processing = 'hillshade' , returnma = False , computeEdges = False ) : if ds is None : ds = mem_ds ( res , extent , srs = None , dtype = gdal . GDT_Float32 ) else : ds = mem_ds_copy ( ds ) b = ds . GetRasterBand ( 1 ) b . WriteArray ( ma ) ... | Wrapper to allow gdaldem calculations for arbitrary NumPy masked array input Untested work in progress placeholder Should only need to specify res can caluclate local gt cartesian srs |
24,222 | def get_xy_ma ( bma , gt , stride = 1 , origmask = True , newmask = None ) : pX = np . arange ( 0 , bma . shape [ 1 ] , stride ) pY = np . arange ( 0 , bma . shape [ 0 ] , stride ) psamp = np . meshgrid ( pX , pY ) mX , mY = pixelToMap ( psamp [ 0 ] , psamp [ 1 ] , gt ) mask = None if origmask : mask = np . ma . getmas... | Return arrays of x and y map coordinates for input array and geotransform |
24,223 | def get_xy_1D ( ds , stride = 1 , getval = False ) : gt = ds . GetGeoTransform ( ) pX = np . arange ( 0 , ds . RasterXSize , stride ) pY = np . arange ( 0 , ds . RasterYSize , stride ) mX , dummy = pixelToMap ( pX , pY [ 0 ] , gt ) dummy , mY = pixelToMap ( pX [ 0 ] , pY , gt ) return mX , mY | Return 1D arrays of x and y map coordinates for input GDAL Dataset |
24,224 | def get_xy_grids ( ds , stride = 1 , getval = False ) : gt = ds . GetGeoTransform ( ) pX = np . arange ( 0 , ds . RasterXSize , stride ) pY = np . arange ( 0 , ds . RasterYSize , stride ) psamp = np . meshgrid ( pX , pY ) mX , mY = pixelToMap ( psamp [ 0 ] , psamp [ 1 ] , gt ) return mX , mY | Return 2D arrays of x and y map coordinates for input GDAL Dataset |
24,225 | def fitPlaneSVD ( XYZ ) : [ rows , cols ] = XYZ . shape p = ( np . ones ( ( rows , 1 ) ) ) AB = np . hstack ( [ XYZ , p ] ) [ u , d , v ] = np . linalg . svd ( AB , 0 ) B = np . array ( v [ 3 , : ] ) coeff = - B [ [ 0 , 1 , 3 ] ] / B [ 2 ] return coeff | Fit a plane to input point data using SVD |
24,226 | def fitPlaneLSQ ( XYZ ) : [ rows , cols ] = XYZ . shape G = np . ones ( ( rows , 3 ) ) G [ : , 0 ] = XYZ [ : , 0 ] G [ : , 1 ] = XYZ [ : , 1 ] Z = XYZ [ : , 2 ] coeff , resid , rank , s = np . linalg . lstsq ( G , Z , rcond = None ) return coeff | Fit a plane to input point data using LSQ |
24,227 | def ds_fitplane ( ds ) : from pygeotools . lib import iolib bma = iolib . ds_getma ( ds ) gt = ds . GetGeoTransform ( ) return ma_fitplane ( bma , gt ) | Fit a plane to values in GDAL Dataset |
24,228 | def getUTMzone ( geom ) : lon , lat = geom . Centroid ( ) . GetPoint_2D ( ) lon180 = ( lon + 180 ) - np . floor ( ( lon + 180 ) / 360 ) * 360 - 180 zonenum = int ( np . floor ( ( lon180 + 180 ) / 6 ) + 1 ) if lat >= 0 : zonehem = 'N' else : zonehem = 'S' if ( lat >= 56.0 and lat < 64.0 and lon180 >= 3.0 and lon180 < 12... | Determine UTM Zone for input geometry |
24,229 | def get_proj ( geom , proj_list = None ) : out_srs = None if proj_list is None : proj_list = gen_proj_list ( ) for projbox in proj_list : if projbox . geom . Intersects ( geom ) : out_srs = projbox . srs break if out_srs is None : out_srs = getUTMsrs ( geom ) return out_srs | Determine best projection for input geometry |
24,230 | def gen_proj_list ( ) : proj_list = [ ] proj_list . append ( ProjBox ( [ - 180 , - 130 , 51.35 , 71.35 ] , 3338 ) ) proj_list . append ( ProjBox ( [ 150 , 175 , - 80 , - 70 ] , 3294 ) ) proj_list . append ( ProjBox ( [ - 180 , 180 , 58 , 82 ] , 3413 ) ) proj_list . append ( ProjBox ( [ - 180 , 180 , - 90 , - 58 ] , 303... | Create list of projections with cascading preference |
24,231 | def xy2geom ( x , y , t_srs = None ) : geom_wkt = 'POINT({0} {1})' . format ( x , y ) geom = ogr . CreateGeometryFromWkt ( geom_wkt ) if t_srs is not None and not wgs_srs . IsSame ( t_srs ) : ct = osr . CoordinateTransformation ( t_srs , wgs_srs ) geom . Transform ( ct ) geom . AssignSpatialReference ( t_srs ) return g... | Convert x and y point coordinates to geom |
24,232 | def get_dem_mosaic_cmd ( fn_list , o , fn_list_txt = None , tr = None , t_srs = None , t_projwin = None , georef_tile_size = None , threads = None , tile = None , stat = None ) : cmd = [ 'dem_mosaic' , ] if o is None : o = 'mos' cmd . extend ( [ '-o' , o ] ) if threads is None : from pygeotools . lib import iolib threa... | Create ASP dem_mosaic command Useful for spawning many single - threaded mosaicing processes |
24,233 | def parse_rs_alg ( r ) : if r == 'near' : gra = gdal . GRA_NearestNeighbour elif r == 'bilinear' : gra = gdal . GRA_Bilinear elif r == 'cubic' : gra = gdal . GRA_Cubic elif r == 'cubicspline' : gra = gdal . GRA_CubicSpline elif r == 'average' : gra = gdal . GRA_Average elif r == 'lanczos' : gra = gdal . GRA_Lanczos eli... | Parse resampling algorithm |
24,234 | def parse_srs ( t_srs , src_ds_list = None ) : if t_srs is None and src_ds_list is None : print ( "Input t_srs and src_ds_list are both None" ) else : if t_srs is None : t_srs = 'first' if t_srs == 'first' and src_ds_list is not None : t_srs = geolib . get_ds_srs ( src_ds_list [ 0 ] ) elif t_srs == 'last' and src_ds_li... | Parse arbitrary input t_srs |
24,235 | def parse_res ( res , src_ds_list = None , t_srs = None ) : t_srs = parse_srs ( t_srs , src_ds_list ) res_str_list = [ 'first' , 'last' , 'min' , 'max' , 'mean' , 'med' , 'common_scale_factor' ] if res in res_str_list and src_ds_list is not None : res_stats = geolib . get_res_stats ( src_ds_list , t_srs = t_srs ) if re... | Parse arbitrary input res |
24,236 | def parse_extent ( extent , src_ds_list = None , t_srs = None ) : if t_srs is not None : t_srs = parse_srs ( t_srs , src_ds_list ) extent_str_list = [ 'first' , 'last' , 'intersection' , 'union' ] if extent in extent_str_list and src_ds_list is not None : if len ( src_ds_list ) == 1 and ( extent == 'intersection' or ex... | Parse arbitrary input extent |
24,237 | def memwarp_multi ( src_ds_list , res = 'first' , extent = 'intersection' , t_srs = 'first' , r = 'cubic' , verbose = True , dst_ndv = 0 ) : return warp_multi ( src_ds_list , res , extent , t_srs , r , warptype = memwarp , verbose = verbose , dst_ndv = dst_ndv ) | Helper function for memwarp of multiple input GDAL Datasets |
24,238 | def memwarp_multi_fn ( src_fn_list , res = 'first' , extent = 'intersection' , t_srs = 'first' , r = 'cubic' , verbose = True , dst_ndv = 0 ) : if not iolib . fn_list_check ( src_fn_list ) : sys . exit ( 'Missing input file(s)' ) src_ds_list = [ gdal . Open ( fn , gdal . GA_ReadOnly ) for fn in src_fn_list ] return mem... | Helper function for memwarp of multiple input filenames |
24,239 | def diskwarp_multi ( src_ds_list , res = 'first' , extent = 'intersection' , t_srs = 'first' , r = 'cubic' , verbose = True , outdir = None , dst_ndv = None ) : return warp_multi ( src_ds_list , res , extent , t_srs , r , verbose = verbose , warptype = diskwarp , outdir = outdir , dst_ndv = dst_ndv ) | Helper function for diskwarp of multiple input GDAL Datasets |
24,240 | def diskwarp_multi_fn ( src_fn_list , res = 'first' , extent = 'intersection' , t_srs = 'first' , r = 'cubic' , verbose = True , outdir = None , dst_ndv = None ) : if not iolib . fn_list_check ( src_fn_list ) : sys . exit ( 'Missing input file(s)' ) src_ds_list = [ gdal . Open ( fn , gdal . GA_ReadOnly ) for fn in src_... | Helper function for diskwarp of multiple input filenames |
24,241 | def writeout ( ds , outfn ) : print ( "Writing out %s" % outfn ) out_ds = iolib . gtif_drv . CreateCopy ( outfn , ds , 0 , options = iolib . gdal_opt ) out_ds = None | Write ds to disk |
24,242 | def getLocalTime ( utc_dt , tz ) : import pytz local_tz = pytz . timezone ( tz ) local_dt = utc_dt . replace ( tzinfo = pytz . utc ) . astimezone ( local_tz ) return local_dt | Return local timezone time |
24,243 | def strptime_fuzzy ( s ) : import dateutil . parser dt = dateutil . parser . parse ( str ( s ) , fuzzy = True ) return dt | Fuzzy date string parsing |
24,244 | def fn_getdatetime_list ( fn ) : fn = os . path . split ( os . path . splitext ( fn ) [ 0 ] ) [ - 1 ] import re dstr = None out = None dstr = re . findall ( r'(?:^|_|-)(?:19|20)[0-9][0-9](?:0[1-9]|1[012])(?:0[1-9]|[12][0-9]|3[01])[_T](?:0[0-9]|1[0-9]|2[0-3])[0-5][0-9]' , fn ) if not dstr : dstr = re . findall ( r'(?:^|... | Extract all datetime strings from input filename |
24,245 | def get_t_factor ( t1 , t2 ) : t_factor = None if t1 is not None and t2 is not None and t1 != t2 : dt = t2 - t1 year = timedelta ( days = 365.25 ) t_factor = abs ( dt . total_seconds ( ) / year . total_seconds ( ) ) return t_factor | Time difference between two datetimes expressed as decimal year |
24,246 | def sort_fn_list ( fn_list ) : dt_list = get_dt_list ( fn_list ) fn_list_sort = [ fn for ( dt , fn ) in sorted ( zip ( dt_list , fn_list ) ) ] return fn_list_sort | Sort input filename list by datetime |
24,247 | def fix_repeat_dt ( dt_list , offset_s = 0.001 ) : idx = ( np . diff ( dt_list ) == timedelta ( 0 ) ) while np . any ( idx ) : dt_list [ idx . nonzero ( ) [ 0 ] + 1 ] += timedelta ( seconds = offset_s ) idx = ( np . diff ( dt_list ) == timedelta ( 0 ) ) return dt_list | Add some small offset to remove duplicate times Needed for xarray interp which expects monotonically increasing times |
24,248 | def get_dt_list ( fn_list ) : dt_list = np . array ( [ fn_getdatetime ( fn ) for fn in fn_list ] ) return dt_list | Get list of datetime objects extracted from a filename |
24,249 | def get_closest_dt_idx ( dt , dt_list ) : from pygeotools . lib import malib dt_list = malib . checkma ( dt_list , fix = False ) dt_diff = np . abs ( dt - dt_list ) return dt_diff . argmin ( ) | Get indices of dt_list that is closest to input dt |
24,250 | def mean_date ( dt_list ) : dt_list_sort = sorted ( dt_list ) dt_list_sort_rel = [ dt - dt_list_sort [ 0 ] for dt in dt_list_sort ] avg_timedelta = sum ( dt_list_sort_rel , timedelta ( ) ) / len ( dt_list_sort_rel ) return dt_list_sort [ 0 ] + avg_timedelta | Calcuate mean datetime from datetime list |
24,251 | def median_date ( dt_list ) : idx = len ( dt_list ) / 2 if len ( dt_list ) % 2 == 0 : md = mean_date ( [ dt_list [ idx - 1 ] , dt_list [ idx ] ] ) else : md = dt_list [ idx ] return md | Calcuate median datetime from datetime list |
24,252 | def dt_cluster ( dt_list , dt_thresh = 16.0 ) : if not isinstance ( dt_list [ 0 ] , float ) : o_list = dt2o ( dt_list ) else : o_list = dt_list o_list_sort = np . sort ( o_list ) o_list_sort_idx = np . argsort ( o_list ) d = np . diff ( o_list_sort ) b = np . nonzero ( d > dt_thresh ) [ 0 ] + 1 b = np . hstack ( ( 0 , ... | Find clusters of similar datetimes within datetime list |
24,253 | def dt2decyear ( dt ) : year = dt . year startOfThisYear = datetime ( year = year , month = 1 , day = 1 ) startOfNextYear = datetime ( year = year + 1 , month = 1 , day = 1 ) yearElapsed = sinceEpoch ( dt ) - sinceEpoch ( startOfThisYear ) yearDuration = sinceEpoch ( startOfNextYear ) - sinceEpoch ( startOfThisYear ) f... | Convert datetime to decimal year |
24,254 | def decyear2dt ( t ) : year = int ( t ) rem = t - year base = datetime ( year , 1 , 1 ) dt = base + timedelta ( seconds = ( base . replace ( year = base . year + 1 ) - base ) . total_seconds ( ) * rem ) return dt | Convert decimal year to datetime |
24,255 | def dt2jd ( dt ) : a = ( 14 - dt . month ) // 12 y = dt . year + 4800 - a m = dt . month + 12 * a - 3 return dt . day + ( ( 153 * m + 2 ) // 5 ) + 365 * y + y // 4 - y // 100 + y // 400 - 32045 | Convert datetime to julian date |
24,256 | def jd2dt ( jd ) : n = int ( round ( float ( jd ) ) ) a = n + 32044 b = ( 4 * a + 3 ) // 146097 c = a - ( 146097 * b ) // 4 d = ( 4 * c + 3 ) // 1461 e = c - ( 1461 * d ) // 4 m = ( 5 * e + 2 ) // 153 day = e + 1 - ( 153 * m + 2 ) // 5 month = m + 3 - 12 * ( m // 10 ) year = 100 * b + d - 4800 + m / 10 tfrac = 0.5 + fl... | Convert julian date to datetime |
24,257 | def gps2dt ( gps_week , gps_ms ) : gps_epoch = datetime ( 1980 , 1 , 6 , 0 , 0 , 0 ) gps_week_s = timedelta ( seconds = gps_week * 7 * 24 * 60 * 60 ) gps_ms_s = timedelta ( milliseconds = gps_ms ) return gps_epoch + gps_week_s + gps_ms_s | Convert GPS week and ms to a datetime |
24,258 | def disco_loop ( opc , version , queue , real_out , dup_lines = False , show_bytes = False ) : while len ( queue ) > 0 : co = queue . popleft ( ) if co . co_name not in ( '<module>' , '?' ) : real_out . write ( "\n" + format_code_info ( co , version ) + "\n" ) bytecode = Bytecode ( co , opc , dup_lines = dup_lines ) re... | Disassembles a queue of code objects . If we discover another code object which will be found in co_consts we add the new code to the list . Note that the order of code discovery is in the order of first encountered which is not amenable for the format used by a disassembler where code objects should be defined before ... |
24,259 | def disco_loop_asm_format ( opc , version , co , real_out , fn_name_map , all_fns ) : if version < 3.0 : co = code2compat ( co ) else : co = code3compat ( co ) co_name = co . co_name mapped_name = fn_name_map . get ( co_name , co_name ) new_consts = [ ] for c in co . co_consts : if iscode ( c ) : if version < 3.0 : c_c... | Produces disassembly in a format more conducive to automatic assembly by producing inner modules before they are used by outer ones . Since this is recusive we ll use more stack space at runtime . |
24,260 | def wr_long ( f , x ) : if PYTHON3 : f . write ( bytes ( [ x & 0xff ] ) ) f . write ( bytes ( [ ( x >> 8 ) & 0xff ] ) ) f . write ( bytes ( [ ( x >> 16 ) & 0xff ] ) ) f . write ( bytes ( [ ( x >> 24 ) & 0xff ] ) ) else : f . write ( chr ( x & 0xff ) ) f . write ( chr ( ( x >> 8 ) & 0xff ) ) f . write ( chr ( ( x >> 16 ... | Internal ; write a 32 - bit int to a file in little - endian order . |
24,261 | def dump_compile ( codeobject , filename , timestamp , magic ) : path_tmp = '%s.%s' % ( filename , id ( filename ) ) fc = None try : fc = open ( path_tmp , 'wb' ) if PYTHON3 : fc . write ( bytes ( [ 0 , 0 , 0 , 0 ] ) ) else : fc . write ( '\0\0\0\0' ) wr_long ( fc , timestamp ) marshal . dump ( codeobject , fc ) fc . f... | Write code object as a byte - compiled file |
24,262 | def int2magic ( magic_int ) : if ( sys . version_info >= ( 3 , 0 ) ) : return struct . pack ( '<Hcc' , magic_int , bytes ( '\r' , 'utf-8' ) , bytes ( '\n' , 'utf-8' ) ) else : return struct . pack ( '<Hcc' , magic_int , '\r' , '\n' ) | Given a magic int like 62211 compute the corresponding magic byte string b \ x03 \ xf3 \ r \ n using the conversion method that does this . |
24,263 | def sysinfo2float ( version_info = sys . version_info ) : vers_str = '.' . join ( [ str ( v ) for v in version_info [ 0 : 3 ] ] ) if version_info [ 3 ] != 'final' : vers_str += '.' + '' . join ( [ str ( i ) for i in version_info [ 3 : ] ] ) if IS_PYPY : vers_str += 'pypy' else : try : import platform platform = platfor... | Convert a sys . versions_info - compatible list into a canonic floating - point number which that can then be used to look up a magic number . Note that this can only be used for released version of C Python not interim development versions since we can t represent that as a floating - point number . |
24,264 | def sysinfo2magic ( version_info = sys . version_info ) : vers_str = '.' . join ( [ str ( v ) for v in version_info [ 0 : 3 ] ] ) if version_info [ 3 ] != 'final' : vers_str += '' . join ( [ str ( v ) for v in version_info [ 3 : ] ] ) if IS_PYPY : vers_str += 'pypy' else : try : import platform platform = platform . py... | Convert a list sys . versions_info compatible list into a canonic floating - point number which that can then be used to look up a magic number . Note that this can raise an exception . |
24,265 | def init_opdata ( l , from_mod , version = None , is_pypy = False ) : if version : l [ 'python_version' ] = version l [ 'is_pypy' ] = is_pypy l [ 'cmp_op' ] = cmp_op l [ 'HAVE_ARGUMENT' ] = HAVE_ARGUMENT if version <= 3.5 : l [ 'findlinestarts' ] = findlinestarts l [ 'findlabels' ] = findlabels l [ 'get_jump_targets' ]... | Sets up a number of the structures found in Python s opcode . py . Python opcode . py routines assign attributes to modules . In order to do this in a modular way here the local dictionary for the module is passed . |
24,266 | def rm_op ( l , name , op ) : l [ 'opname' ] [ op ] = '<%s>' % op if op in l [ 'hasconst' ] : l [ 'hasconst' ] . remove ( op ) if op in l [ 'hascompare' ] : l [ 'hascompare' ] . remove ( op ) if op in l [ 'hascondition' ] : l [ 'hascondition' ] . remove ( op ) if op in l [ 'hasfree' ] : l [ 'hasfree' ] . remove ( op ) ... | Remove an opcode . This is used when basing a new Python release off of another one and there is an opcode that is in the old release that was removed in the new release . We are pretty aggressive about removing traces of the op . |
24,267 | def opcode_check ( l ) : if ( abs ( PYTHON_VERSION - l [ 'python_version' ] ) <= 0.01 and IS_PYPY == l [ 'is_pypy' ] ) : try : import dis opmap = fix_opcode_names ( dis . opmap ) assert all ( item in opmap . items ( ) for item in l [ 'opmap' ] . items ( ) ) assert all ( item in l [ 'opmap' ] . items ( ) for item in opm... | When the version of Python we are running happens to have the same opcode set as the opcode we are importing we perform checks to make sure our opcode set matches exactly . |
24,268 | def dump_opcodes ( opmap ) : op2name = { } for k in opmap . keys ( ) : op2name [ opmap [ k ] ] = k for i in sorted ( op2name . keys ( ) ) : print ( "%-3s %s" % ( str ( i ) , op2name [ i ] ) ) | Utility for dumping opcodes |
24,269 | def pretty_flags ( flags ) : names = [ ] result = "0x%08x" % flags for i in range ( 32 ) : flag = 1 << i if flags & flag : names . append ( COMPILER_FLAG_NAMES . get ( flag , hex ( flag ) ) ) flags ^= flag if not flags : break else : names . append ( hex ( flags ) ) names . reverse ( ) return "%s (%s)" % ( result , " |... | Return pretty representation of code flags . |
24,270 | def _try_compile ( source , name ) : try : c = compile ( source , name , 'eval' ) except SyntaxError : c = compile ( source , name , 'exec' ) return c | Attempts to compile the given source first as an expression and then as a statement if the first approach fails . |
24,271 | def get_code_object ( x ) : if hasattr ( x , '__func__' ) : x = x . __func__ if hasattr ( x , '__code__' ) : x = x . __code__ if hasattr ( x , 'gi_code' ) : x = x . gi_code if isinstance ( x , str ) : x = _try_compile ( x , "<disassembly>" ) if hasattr ( x , 'co_code' ) : return x raise TypeError ( "don't know how to d... | Helper to handle methods functions generators strings and raw code objects |
24,272 | def get_jump_target_maps ( code , opc ) : offset2prev = { } prev_offset = - 1 for offset , op , arg in unpack_opargs_bytecode ( code , opc ) : if prev_offset >= 0 : prev_list = offset2prev . get ( offset , [ ] ) prev_list . append ( prev_offset ) offset2prev [ offset ] = prev_list if op in opc . NOFOLLOW : prev_offset ... | Returns a dictionary where the key is an offset and the values are a list of instruction offsets which can get run before that instruction . This includes jump instructions as well as non - jump instructions . Therefore the keys of the dictionary are reachable instructions . The values of the dictionary may be useful i... |
24,273 | def _get_const_info ( const_index , const_list ) : argval = const_index if const_list is not None : argval = const_list [ const_index ] if isinstance ( argval , float ) and str ( argval ) in frozenset ( [ 'nan' , '-nan' , 'inf' , '-inf' ] ) : return argval , "float('%s')" % argval return argval , repr ( argval ) | Helper to get optional details about const references |
24,274 | def _get_name_info ( name_index , name_list ) : argval = name_index if ( name_list is not None and name_index < len ( name_list ) ) : argval = name_list [ name_index ] argrepr = argval else : argrepr = repr ( argval ) return argval , argrepr | Helper to get optional details about named references |
24,275 | def instruction_size ( op , opc ) : if op < opc . HAVE_ARGUMENT : return 2 if opc . version >= 3.6 else 1 else : return 2 if opc . version >= 3.6 else 3 | For a given opcode op in opcode module opc return the size in bytes of an op instruction . |
24,276 | def disassemble ( self , lineno_width = 3 , mark_as_current = False , asm_format = False , show_bytes = False ) : fields = [ ] if asm_format : indexed_operand = set ( [ 'name' , 'local' , 'compare' , 'free' ] ) if lineno_width : if self . starts_line is not None : if asm_format : lineno_fmt = "%%%dd:\n" % lineno_width ... | Format instruction details for inclusion in disassembly output |
24,277 | def from_traceback ( cls , tb ) : while tb . tb_next : tb = tb . tb_next return cls ( tb . tb_frame . f_code , current_offset = tb . tb_lasti ) | Construct a Bytecode from the given traceback |
24,278 | def dis ( self , asm_format = False , show_bytes = False ) : co = self . codeobj if self . current_offset is not None : offset = self . current_offset else : offset = - 1 output = StringIO ( ) self . disassemble_bytes ( co . co_code , varnames = co . co_varnames , names = co . co_names , constants = co . co_consts , ce... | Return a formatted view of the bytecode operations . |
24,279 | def get_jump_target_maps ( code , opc ) : offset2prev = { } prev_offset = - 1 for offset , op , arg in unpack_opargs_wordcode ( code , opc ) : if prev_offset >= 0 : prev_list = offset2prev . get ( offset , [ ] ) prev_list . append ( prev_offset ) offset2prev [ offset ] = prev_list prev_offset = offset if op in opc . NO... | Returns a dictionary where the key is an offset and the values are a list of instruction offsets which can get run before that instruction . This includes jump instructions as well as non - jump instructions . Therefore the keys of the dictionary are reachible instructions . The values of the dictionary may be useful i... |
24,280 | def dis ( self , x = None , file = None ) : self . _print ( self . Bytecode ( x ) . dis ( ) , file ) | Disassemble classes methods functions generators or code . |
24,281 | def get_protocol_from_name ( name ) : cls = protocol_map . get ( name ) if not cls : raise ValueError ( 'Unsupported protocol "%s".' % name ) return cls | Returns the protocol class for the protocol with the given name . |
24,282 | def create_protocol ( name , ** kwargs ) : cls = protocol_map . get ( name ) if not cls : raise ValueError ( 'Unsupported protocol "%s".' % name ) return cls ( ** kwargs ) | Returns an instance of the protocol with the given name . |
24,283 | def _long_from_raw ( thehash ) : hashnum = 0 for h in thehash : hashnum <<= 8 hashnum |= ord ( bytes ( [ h ] ) ) return hashnum | Fold to a long a digest supplied as a string . |
24,284 | def aborted ( self , exc_info ) : self . exc_info = exc_info self . did_end = True self . write ( format_exception ( * self . exc_info ) ) | Called by a logger to log an exception . |
24,285 | def from_file ( filename , password = '' , keytype = None ) : if keytype is None : try : key = RSAKey . from_private_key_file ( filename ) keytype = 'rsa' except SSHException as e : try : key = DSSKey . from_private_key_file ( filename ) keytype = 'dss' except SSHException as e : msg = 'not a recognized private key: ' ... | Returns a new PrivateKey instance with the given attributes . If keytype is None we attempt to automatically detect the type . |
24,286 | def get_uri ( self ) : url = Url ( ) url . protocol = self . get_protocol ( ) url . hostname = self . get_address ( ) url . port = self . get_tcp_port ( ) url . vars = dict ( ( k , to_list ( v ) ) for ( k , v ) in list ( self . get_all ( ) . items ( ) ) if isinstance ( v , str ) or isinstance ( v , list ) ) if self . a... | Returns a URI formatted representation of the host including all of it s attributes except for the name . Uses the address not the name of the host to build the URI . |
24,287 | def set_address ( self , address ) : if is_ip ( address ) : self . address = clean_ip ( address ) else : self . address = address | Set the address of the remote host the is contacted without changing hostname username password protocol and TCP port number . This is the actual address that is used to open the connection . |
24,288 | def get_option ( self , name , default = None ) : if self . options is None : return default return self . options . get ( name , default ) | Returns the value of the given option if it is defined returns the given default value otherwise . |
24,289 | def set_tcp_port ( self , tcp_port ) : if tcp_port is None : self . tcp_port = None return self . tcp_port = int ( tcp_port ) | Defines the TCP port number . |
24,290 | def append ( self , name , value ) : if self . vars is None : self . vars = { } if name in self . vars : self . vars [ name ] . append ( value ) else : self . vars [ name ] = [ value ] | Appends the given value to the list variable with the given name . |
24,291 | def get ( self , name , default = None ) : if self . vars is None : return default return self . vars . get ( name , default ) | Returns the value of the given variable or the given default value if the variable is not defined . |
24,292 | def copy_labels ( src , dst ) : labels = src . __dict__ . get ( '_labels' ) if labels is None : return dst . __dict__ [ '_labels' ] = labels . copy ( ) | Copies all labels of one object to another object . |
24,293 | def serializeable_exc_info ( thetype , ex , tb ) : return thetype , ex , '' . join ( traceback . format_exception ( thetype , ex , tb ) ) | Since traceback objects can not be pickled this function manipulates exception info tuples before they are passed accross process boundaries . |
24,294 | def deprecated ( func ) : def decorated ( * args , ** kwargs ) : warnings . warn ( 'Call to deprecated function %s.' % func . __name__ , category = DeprecationWarning , stacklevel = 2 ) return func ( * args , ** kwargs ) decorated . __name__ = func . __name__ decorated . __doc__ = func . __doc__ decorated . __dict__ . ... | A decorator for marking functions as deprecated . Results in a printed warning message when the function is used . |
24,295 | def synchronized ( func ) : @ wraps ( func ) def wrapped ( self , * args , ** kwargs ) : try : rlock = self . _sync_lock except AttributeError : from multiprocessing import RLock rlock = self . __dict__ . setdefault ( '_sync_lock' , RLock ( ) ) with rlock : return func ( self , * args , ** kwargs ) return wrapped | Decorator for synchronizing method access . |
24,296 | def debug ( func ) : @ wraps ( func ) def wrapped ( * args , ** kwargs ) : arg = repr ( args ) + ' ' + repr ( kwargs ) sys . stdout . write ( 'Entering ' + func . __name__ + arg + '\n' ) try : result = func ( * args , ** kwargs ) except : sys . stdout . write ( 'Traceback caught:\n' ) sys . stdout . write ( format_exce... | Decorator that prints a message whenever a function is entered or left . |
24,297 | def eval ( conn , string , strip_command = True , ** kwargs ) : parser_args = { 'strip_command' : strip_command } return _run ( conn , None , string , parser_args , ** kwargs ) | Compiles the given template and executes it on the given connection . Raises an exception if the compilation fails . |
24,298 | def _urlparse_qs ( url ) : querystring = urlparse ( url ) [ 4 ] pairs = [ s2 for s1 in querystring . split ( '&' ) for s2 in s1 . split ( ';' ) ] result = OrderedDefaultDict ( list ) for name_value in pairs : pair = name_value . split ( '=' , 1 ) if len ( pair ) != 2 : continue if len ( pair [ 1 ] ) > 0 : name = _unquo... | Parse a URL query string and return the components as a dictionary . |
24,299 | def set_debug ( self , debug = 1 ) : self . _check_if_ready ( ) self . debug = debug self . main_loop . debug = debug | Set the debug level . |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.