dup

每周一个 Python 标准库 | copy

大憨熊 提交于 2020-01-22 05:33:54
技术博客:https://github.com/yongxinz/tech-blog 同时,也欢迎关注我的微信公众号 AlwaysBeta ,更多精彩内容等你来。 copy 模块包括两个功能, copy() 和 deepcopy() ,用于复制现有对象。 浅拷贝 copy() 创建的浅表副本是一个新容器,是对原始对象内容的引用。 import copy import functools @functools . total_ordering class MyClass : def __init__ ( self , name ) : self . name = name def __eq__ ( self , other ) : return self . name == other . name def __gt__ ( self , other ) : return self . name > other . name a = MyClass ( 'a' ) my_list = [ a ] dup = copy . copy ( my_list ) print ( ' my_list:' , my_list ) print ( ' dup:' , dup ) print ( ' dup is my_list:' , ( dup is my_list ) ) print ( '

汇编语言第七、八章总结

无人久伴 提交于 2020-01-10 01:45:29
and和or指令 1. and指令——逻辑与指令,按位进行与运算,通过该指令可将操作对象的相应位设为0,其他位不变 2. or指令——逻辑或指令,按位进行或运算,通过该指令可将操作对象的相应位设为1,其他位不变 以字符形式给出的数据 1. 以“...”的方式指明数据是以字符的形式给出的,编译器把它们转化为相应的ASCII码 大小写转换的问题 1. 大写字母+20H=小写字母;小写字母-20H=大写字母 2. and al,11011111B 可将小写字母变为大写字母;or al,00100000B 可将大写字母变为小写字母 [bx+idata] 1. 表示一个内存单元,它的偏移地址为(bx)+idata(bx中的数值加上idata) si和di 1. 是和bx功能相近的寄存器,但是si和di不能够分成两个8位寄存器来使用 [bx+si]和[bx+di] 1. [bx+si]表示一个内存单元,它的偏移地址为(bx)+(si)(即bx中的数值加上si中的数值) [bx+si+idata]和[bx+di+idata] 1. [bx+si+idata]表示一个内存单元,它的偏移地址为(bx)+(si)+idata(即bx中的数值加上si中的数值再加上idata) ( 一般来说,在需要暂存数据的时候,我们都应该使用栈 ) bx、si、di和bp 1. 只有这四个寄存器可以用在[...

Redirect stdout from python for C calls

社会主义新天地 提交于 2019-12-28 17:41:17
问题 This is a follow up question from here specifically concerning its answer. From a python module I am calling a Hello World executable that simply prints Hello World to the stdout. I am interested in redirecting that output to a python StringIO and ran into this answer which almost brings me all the way to the solution. The critical part of this answer is this code segment: 1. def redirect_stdout(): 2. print "Redirecting stdout" 3. sys.stdout.flush() # <--- important when redirecting to files

Simple http generic server using fork and dup

戏子无情 提交于 2019-12-24 16:27:00
问题 I'm trying to write a very basic HTTP server. I want to separate the server and the service in two separate executable (similar to inetd). So I have a generic server, that forks a service and execute its code using exec. The main loop is as follows: while(true) { int client_sock; if ((client_sock = accept(server_sock, (struct sockaddr *) NULL, NULL)) < 0) { return -1; } pid_t pid = fork(); if (pid == 0) { close(server_sock); close(0); dup(client_sock); close(1); dup(client_sock); execvp(argv

Strange behaviour when redirecting stdout in C

做~自己de王妃 提交于 2019-12-22 18:39:51
问题 I'm trying to redirect stdout to a file and then restore it back to original in C, but I'm facing the following strange issue - the following piece of code succesfully writes in stdout in stdout in stdout and in file in the respective file which is all OK. #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #define STDOUT 1 int main(int argc, char* argv[]){ printf("in stdout \n"); int old_out = dup(STDOUT); close(STDOUT); int fd = open("./redirected",O_CREAT|O_RDWR|O_TRUNC,0777); printf

C Shell hanging when dealing with piping

亡梦爱人 提交于 2019-12-20 06:28:06
问题 I'm working on a C shell and am having trouble with getting an arbitrary amount of pipes to work. When I run the shell, it hangs on any piping. For some reason, when I do ls -la | sort , it hangs on the sort until I enter stuff and hit Ctrl + D . I know it has something to do with a pipe not closing, but the print statements show that pipes 3,4,5 all get closed in both the parent and child. I've been at this for a few hours and don't know why this doesn't work. Any help would be much

How to redirect the output of system() to a file?

自古美人都是妖i 提交于 2019-12-18 16:57:21
问题 In this C program #include <stdio.h> #include <fcntl.h> int main() { int file = open("Result", O_CREAT|O_WRONLY, S_IRWXU); dup2(stdout, file); system("ls -l"); return 0; } I'm trying to redirect the output of system() to a file, for that i have used dup2 but it is not working. What's wrong with this code ? and, please tell me if there is any better way to do this ? (without using > at the terminal ) 回答1: stdout is a FILE * pointer of the standard output stream. dup2 expects file descriptor,

数据处理的两个基本问题

北慕城南 提交于 2019-12-18 08:15:15
计算机是进行数据处理,运算的机器,所以存在两个问题: 处理的数据的位置 处理的数据的长度 这两个问题,必须在机器指令中给出说明(有时候是明确的,有时候是隐式的),否者计算器就无法工作。 定义的描述性符号: reg(寄存器):ax,bx,cx,dx,ah,al···sp,bp,si,di sreg(段寄存器):ds,ss,cs,es bx,si,di和bp 总结: 在8086中, 只有 这四个寄存器可以用在 [...] 中进行内存寻址。 在 [...] 中,他们可以单个出现,或者以组合形式出现(组合中不能有其他寄存器,但可以有idata) bp的默认段地址在ss中(和bx,si,di在ds中不同) 机器指令处理的数据在什么地方 数据处理大致可分为三类:读取,写入,运算 指令执行前,所要处理的数据可以在3个地方:cpu内部,内存,端口 汇编语言中数据位置的表达 立即数(idata) 对于直接包含在机器指令中的数据(执行前在cpu的指令缓冲器中),在汇编语言中称为:立即数(idata) 寄存器 指令要处理的数据存放在寄存器中,给出对应寄存器名 段地址(SA)和偏移地址(EA) 内存中的数据,通过SA+EA给出数据位置 寻址方式 定位内存单元的方法,即称为寻址方式 指令要处理的数据有多长 前面将到分为隐式给出和显式给出,例如通过寄存器和push,pop等方法就是隐式给出。

Ruby dup/clone recursively

試著忘記壹切 提交于 2019-12-17 19:07:04
问题 I have a hash like: h = {'name' => 'sayuj', 'age' => 22, 'project' => {'project_name' => 'abc', 'duration' => 'prq'}} I need a dup of this hash, the change should not affect the original hash. When I try, d = h.dup # or d = h.clone d['name'] = 'sayuj1' d['project']['duration'] = 'xyz' p d #=> {"name"=>"sayuj1", "project"=>{"duration"=>"xyz", "project_name"=>"abc"}, "age"=>22} p h #=> {"name"=>"sayuj", "project"=>{"duration"=>"xyz", "project_name"=>"abc"}, "age"=>22} Here you can see the

practical examples use dup or dup2

自闭症网瘾萝莉.ら 提交于 2019-12-17 07:01:39
问题 I know what dup / dup2 does, but I have no idea when it would be used. Any practical examples? Thanks. 回答1: One example use would be I/O redirection. For this you fork a child process and close the stdin or stdout file descriptors (0 and 1) and then you do a dup() on another filedescriptor of your choice which will now be mapped to the lowest available file descriptor, which is in this case 0 or 1. Using this you can now exec any child process which is possibly unaware of your application and