洛卜哒的博客

Hope I can study from bits to qubits.


  • 首页

  • 分类

  • 关于

  • 归档

  • 标签

获取当前shell执行脚本的名字的几种方法

发表于 2017-03-12   |   分类于 Linux

$0不够用?

当我们编写shell脚本时,有时候需要获取当前执行脚本的名字,最常用的应该就是$0了,但是根据执行脚本的方式不同,这种方式是有缺陷的,我们执行脚本的可能方式有:

1
2
3
./script.sh
. script.sh
source script.sh

这几种方式的$0不尽相同,先总结如下:

方法 描述
$0 only works when user executes “./script.sh”
$BASH_ARGV only works when user executes “. script.sh” or “source script.sh”
${BASH_SOURCE[0]} works on both cases.
readlink -f useful when symbolic link is used

关于添加双下划线构成python私有成员(私有函数,私有变量)

发表于 2017-03-07   |   分类于 python

我们都是成年人

we’re all consenting adults here —- What is “pythonic”?

python中并没有真正意义上的私有成员,它提供了在成员前面添加双下划线的方法来模拟类似功能。具体来说:

  • _xxx 表示模块级别的私有变量或函数
  • __xxx 表示类的私有变量或函数

这被称为name nangling,将__membername替换为_classname__membername。
这样从语法上实现了私有成员不能用classinstance.__membername访问,但是任然可以用
classinstance._classname__membername访问。

阅读全文 »

python3源码浅读——对象PyObject

发表于 2017-03-05   |   分类于 python

对象CPython声明

Object and type object interface

首先我们先来看看object.h文件中的注释。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* Object and type object interface */
/*
Objects are structures allocated on the heap. Special rules apply to
the use of objects to ensure they are properly garbage-collected.
Objects are never allocated statically or on the stack; they must be
accessed through special macros and functions only. (Type objects are
exceptions to the first rule; the standard types are represented by
statically initialized type objects, although work on type/class unification
for Python 2.2 made it possible to have heap-allocated type objects too).
An object has a 'reference count' that is increased or decreased when a
pointer to the object is copied or deleted; when the reference count
reaches zero there are no references to the object left and it can be
removed from the heap.
An object has a 'type' that determines what it represents and what kind
of data it contains. An object's type is fixed when it is created.
Types themselves are represented as objects; an object contains a
pointer to the corresponding type object. The type itself has a type
pointer pointing to the object representing the type 'type', which
contains a pointer to itself!).
Objects do not float around in memory; once allocated an object keeps
the same size and address. Objects that must hold variable-size data
can contain pointers to variable-size parts of the object. Not all
objects of the same type have the same size; but the size cannot change
after allocation. (These restrictions are made so a reference to an
object can be simply a pointer -- moving an object would require
updating all the pointers, and changing an object's size would require
moving it if there was another object right next to it.)
Objects are always accessed through pointers of the type 'PyObject *'.
The type 'PyObject' is a structure that only contains the reference count
and the type pointer. The actual memory allocated for an object
contains other data that can only be accessed after casting the pointer
to a pointer to a longer structure type. This longer type must start
with the reference count and type fields; the macro PyObject_HEAD should be
used for this (to accommodate for future changes). The implementation
of a particular object type can cast the object pointer to the proper
type and back.
A standard interface exists for objects that contain an array of items
whose size is determined when the object is allocated.
*/

简单总结如下:

  • 对象分配一般在堆上进行
  • 每个对象有一个reference count 引用计数
  • 每一个对象有一个type来真正表明对象类型
  • 对象一旦分配,其在内存的地址就不在改变
  • 任何对象可以通过PyObject *类型指针访问,PyObject结构只包含引用计数和type类型指针两个字段,实际的对象数据由type类型指针指向,即_typeobject结构体。
阅读全文 »

python3源码浅读——编译

发表于 2017-03-05   |   分类于 python

python3源码编译安装

Python3源码结构

  • Python源码目录结构

  • Include:Python所有的头文件,写C/C++扩展时需要

  • Lib:Python自带的标准库,Python语言编写
  • Modules:由C语言编写的模块,如ctypes、mutltiprocessing等
  • Parser: Python解释器的Scanner和Parser,即Python的词法分析和语法分析
  • Objects:Python的内建对象的实现、包括list、dict等
  • Python:Python解释器的Compiler和执行引擎
  • PCBuild:Visual Studio工程文件
阅读全文 »

python中如何实现单例模式

发表于 2015-04-15   |   分类于 python

python中实现Singleton模式

主要参考来自于stackoverflow

阅读全文 »

python的chained assignment

发表于 2015-04-14   |   分类于 python

python小知识关于a=b=True是怎样工作的

链式赋值关于python有一个有趣的地方,从上面这段话我们可以了解到:

1、在C语言中,a=b=True等价于

1
2
b = True
a = b

2、而在python中,a = b = True等价于

1
2
3
4
temporary_expr_result = True
a = temporary_expr_result
b = temporary_expr_result
阅读全文 »

Trie_tree

发表于 2015-04-11   |   分类于 algorithm

##基本算法学习笔记之Trie树

又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存
大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来减少查询时间,最大限
度地减少无谓的字符串比较,查询效率比哈希树高,空间换时间的算法。
使用范围:词频统计、前缀匹配

阅读全文 »

cpython整型溢出

发表于 2015-01-19   |   分类于 c,python

##C语言中的整型溢出

如果说我比别人看得更远些,那是因为我站在了巨人的肩上. ——牛顿

在C语言中,整型溢出分为有符号和无符号两种:

1. signed integer overflow : undefined behavior
2. unsigned integer overflow : safely wraps around (UINT_MAX + 1 yeild 0)

对于unsigned整型溢出,C的规范是有定义的——“溢出后的数会以2^(8*sizeof(type))作模运算”;对于signed整型的溢出,C的规范定义是“undefined behavior”。详细介绍请参考Basics of Integer Overflow和C语言的整型溢出问题。

阅读全文 »

python中的__class__

发表于 2015-01-16   |   分类于 python

##python中的__class__

__class__是类的一个内置属性,也是每个类实例的,它是一个类的引用。

self.__class__ is a reference to the type of the current instance.

简单例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
'''类.__class__总是返回<type'type'>;实例.__class__返回实例所属的类的一个引用。'''
class A(object):
pass
>>> A.__class__
<type 'type'>
>>> a=A()
>>> a
<__main__.A object at 0x01B91690>
>>> a.__class__
<class '__main__.A'>
>>> a.__class__() #返回了类的一个新的实例
<__main__.A object at 0x01B915D0>
>>> A.__class__(A) #与A.__class__一样
<type 'type'>

可以从这里看到从CPython源码来分析__class__具体干了什么。

阅读全文 »

C语言中的数值

发表于 2015-01-14   |   分类于 C

##数值的类型

C语言中的数值类型有

整型:short int,int, long int
浮点型:float, double
字符型:char

其中又分为有符号数signed和无符号数unsigned。
其实数值在内存中存储时只是一串二进制串,没有正负之分,所谓的正负是如何去解释这个二进制串。例如:

1
2
unsigned int a = -1;
printf("%u %d\n", a, a);

我们指定%u和%d两种方式输出,得到结果:4294967295 -1
我们定义变量的类型是给编译器看的,而变量本身只是内存空间,你想让它后来解释成什么类型就解释成什么类型。

阅读全文 »
1…34
洛卜哒

洛卜哒

大约,孔乙己的确死了!

40 日志
11 分类
11 标签
GitHub
  • Github
  • Project Euler
© 2023 洛卜哒
由 Hexo 强力驱动
主题 - NexT.Muse