Skip to content

Commit

Permalink
1. tiny fix for progress_bar
Browse files Browse the repository at this point in the history
  1. tiny fix for `ostap.utils.basic.isatty`
  • Loading branch information
VanyaBelyaev committed Sep 3, 2024
1 parent ecf379e commit 0cddcd7
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 86 deletions.
4 changes: 3 additions & 1 deletion ReleaseNotes/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
## Bug fixes

1. fix the typo in `ostap/tools/tests/test_tools_reweight3.py`

1. tiny fix for `progress_bar`
1. tiny fix for `ostap.utils.basic.isatty`

# v1.13.0.2

## New features
Expand Down
99 changes: 63 additions & 36 deletions ostap/utils/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,30 @@
# if isatty( stream ) : print('Teminal!')
# @endcode
def isatty ( stream = None ) :
"""Is the stream is attached to terminal?
""" Is the stream is attached to terminal?
>>> stream = ...
>>> if isatty( stream ) : print('Teminal!')
>>> if isatty() : print('stdout is terminal!')
"""
if not stream : stream = sys.stdout
#
try :
return sys.stdout.isatty()
except : pass
#
try :
return os.isatty ( sys.stdout.fileno() )
except : pass
#
##
if hasattr ( stream , 'isatty' ) :
try : return stream.isatty()
except : pass
##
if hasattr ( stream , 'fileno' ) :
try : return os.isatty ( stream.fileno () )
except : pass
##
return False

# =============================================================================
## Who am i ?
## Who am I ?
# @cdoe
# print ( 'I am :' % whoami() )
# @endcode
def whoami () :
""" Who am i ?
""" Who am I ?
>>> print ( "I am ", whoami() )
"""
try :
Expand All @@ -84,7 +84,7 @@ def whoami () :
# =============================================================================
## helper function that allows to detect running ipython
def with_ipython() :
"""Helper function that allows to detect running ipython"""
""" Helper function that allows to detect running ipython"""
try :
return __IPYTHON__
except NameError :
Expand All @@ -93,7 +93,7 @@ def with_ipython() :
# ============================================================================
## Get the terminal console size
def terminal_size():
"""Get the terminal console size
""" Get the terminal console size
>>> height , width = terminal_size()
"""
try :
Expand All @@ -113,7 +113,7 @@ def terminal_size():
# make_dir( path )
# @endcode
def make_dir ( bdir ) :
"""Make new directory
""" Make new directory
>>> path = ...
>>> make_dir ( path )
"""
Expand All @@ -135,7 +135,7 @@ def make_dir ( bdir ) :
# if wrietable ( my_dir ) : ...
# @endcode
def writeable ( adir ) :
"""Is this directory is writeable?
""" Is this directory is writeable?
>>> my_dir = ...
>>> if writeable ( my_dir ) : ...
"""
Expand Down Expand Up @@ -172,13 +172,13 @@ def commonpath ( paths ) :

# =============================================================================
if (3,2) <= sys.version_info :

# =========================================================================
make_dirs = os.makedirs

# =========================================================================
else :

# =========================================================================
def make_dirs ( name , mode = 0o777 , exist_ok = False ):
"""makedirs(path [, mode=0o777])
""" makedirs(path [, mode=0o777])
Super-mkdir; create a leaf directory and all intermediate ones.
Works like mkdir, except that any intermediate path segment (not
Expand Down Expand Up @@ -215,7 +215,6 @@ def make_dirs ( name , mode = 0o777 , exist_ok = False ):
if not exist_ok or not os.path.isdir ( name ) :
raise


# =============================================================================
## @class NoContext
# Fake empty context manager to be used as empty placeholder
Expand All @@ -238,15 +237,28 @@ def __exit__ ( self , *args ) : pass


# =============================================================================
if ( 3 , 0 ) <= sys.version_info : items_loop = lambda d : d.items ()
else : items_loop = lambda d : d.iteritems ()
if ( 3 , 0 ) <= sys.version_info :
# =========================================================================
def loop_items ( dct ) :
""" Iterate over the dictionary items
>>> d = { 'a' : ... , 'b' : ... , }
>>> for e in loop_items ( d ) : print (e)
"""
for item in dct.items () : yield item
# =========================================================================
else :
# =========================================================================
def loop_items ( dct ) :
""" Iterate over the dictionary items
>>> d = { 'a' : ... , 'b' : ... , }
>>> for e in loop_items ( d ) : print (e)
"""
for item in dct.iteritems () : yield item
# =========================================================================

# =============================================================================
def loop_items ( d ) :
"""Return iterator over the dictionary items
>>> d = { 'a' : ... , 'b' : ... , }
>>> for e in loop_items ( d ) : print (e)
"""
return items_loop ( d )
## Iterate over the dictionary items
items_loop = loop_items

# =============================================================================
## case-insensitive check for existence of the environment variable
Expand All @@ -257,8 +269,7 @@ def loop_items ( d ) :
# has_env ( 'Ostap_Table_Style' )
# @endcode
def has_env ( variable ) :
"""
Case-insensitive check for existence of the environment variable
""" Case-insensitive check for existence of the environment variable
- case-insensitive
- space ignored
Expand Down Expand Up @@ -392,7 +403,7 @@ def copy_file ( source ,
def sync_file ( source ,
destination ,
progress = False ) :
"""Sync/Copy source file into destination, creating intermediate directories
""" Sync/Copy source file into destination, creating intermediate directories
- see https://stackoverflow.com/questions/2793789/create-destination-path-for-shutil-copy-files/49615070
"""
from ostap.utils.utils import which
Expand Down Expand Up @@ -426,13 +437,29 @@ def sync_file ( source ,
return destination

# =============================================================================
if ( 3 , 4 ) <= sys.version_info :
if ( 3 , 4 ) <= sys.version_info :
# =========================================================================
## Get number of cores/CPUs
from os import cpu_count as numcpu
from os import cpu_count as _numcpu
# =========================================================================
else :
## Get number of cores/CPUs
from multiprocessing import cpu_count as numcpu
# =========================================================================
## Get number of cores/CPUs
from multiprocessing import cpu_count as _numcpu
# =========================================================================

# =============================================================================
## Get number of CPUs
def numcpu () :
""" Get number of CPUs (non-negative integer number)
- it uses the function `cpu_count` from `%s` module
"""
nc = _numcpu ()
return nc if nc and 0 < nc else 0

numcpu.__doc__ = numcpu.__doc__ % _numcpu.__module__ \
+ '\n' + _numcpu.__doc__

# =============================================================================
if __name__ == '__main__' :

Expand Down
Loading

0 comments on commit 0cddcd7

Please sign in to comment.